I have always known that all good programmers call Dispose on any object that implements IDisposable, case in point the ObjectContext class in EF.
I am new to asp.net mvc so this may be a noob question but here goes…
public ActionResult Index()
{
using (var db = new MyObjectContext())
{
return View(db.People);
}
}
If I run this code I get an error (ObjectDisposedException) because the ObjectContext has been disposed of before the View takes action on the data. Is there a different approach here? How can I ensure my objects get disposed of as soon as possible?
Override the Controller.Dispose() method and dispose of your object in there:
The MVC framework will automatically call Controller.Dispose() at the end of the request.