Hi people am having a problem am getting the following error:
The ObjectContext instance has been disposed and can no longer be used for operations that require a connection.
and the error is directed at the following:
@Html.DisplayFor(modelItem => item.tblConsole.ConsoleName) "within the game table"
I have the following code in my game controller:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GameTest.Models;
namespace GameTest.Controllers
{
public class GameController : Controller
{
private gamezoneDBEntities db = new gamezoneDBEntities();
//
// GET: /Game/
public ViewResult Index()
{
using (var db = new gamezoneDBEntities())
{
var Info = db.tblGames.Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
return View(Info);
}
}
//
// GET: /Game/Details/5
public ViewResult Details(int id)
{
tblGame tblgame = db.tblGames.Find(id);
return View(tblgame);
}
//
// GET: /Game/Create
public ActionResult Create()
{
ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName");
return View(new tblGame { UserName = @User.Identity.Name });
}
//
// POST: /Game/Create
[HttpPost]
public ActionResult Create(tblGame tblgame)
{
if (ModelState.IsValid)
{
db.tblGames.Add(tblgame);
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName", tblgame.ConsoleNameIDFK);
return View(tblgame);
}
//
// GET: /Game/Edit/5
public ActionResult Edit(int id)
{
tblGame tblgame = db.tblGames.Find(id);
ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName", tblgame.ConsoleNameIDFK);
return View(tblgame);
}
//
// POST: /Game/Edit/5
[HttpPost]
public ActionResult Edit(tblGame tblgame)
{
if (ModelState.IsValid)
{
db.Entry(tblgame).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
ViewBag.ConsoleNameIDFK = new SelectList(db.tblConsoles, "ConsoleName", "ConsoleName", tblgame.ConsoleNameIDFK);
return View(tblgame);
}
//
// GET: /Game/Delete/5
public ActionResult Delete(int id)
{
tblGame tblgame = db.tblGames.Find(id);
return View(tblgame);
}
//
// POST: /Game/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmed(int id)
{
tblGame tblgame = db.tblGames.Find(id);
db.tblGames.Remove(tblgame);
db.SaveChanges();
return RedirectToAction("Index");
}
protected override void Dispose(bool disposing)
{
db.Dispose();
base.Dispose(disposing);
}
}
}
I have used this code in the index so each game is unique to the user who posted the game:
var Info = db.tblGames.Where(UserInfo => UserInfo.UserName.Equals(User.Identity.Name)).ToList();
return View(Info);
and have added this in the first create section of my controller:
return View(new tblGame { UserName = @User.Identity.Name });
This code does work when you don’t click on create and every user has individual posts of games but when I press create new it crashes
Any help would be appreciated. Please ask if you need to know more
You need to
Includethe console table data in your query.Your returned entity is trying to
lazy loadin additional data but cannot because the data context has already been disposed. This is because you wrapped it in ausingblock.By using
Includeyou areEager Loadingall the data you need in the initial query and the entity will not need to make any additional queries to get the data you are requesting in the view.You can read more here on
EagerandLazyloading: http://msdn.microsoft.com/en-us/library/bb896272.aspxAlso, there is no reason to have the private instance of gamezoneDBEntities:
Just use the instance you create in the
usingblock: