Entity Framework DataContext Concern - Is it getting disposed properly in my controller?
asp.net-mvc, c#, entity-framework
Solution
The Controller class has a method of dispose. So override that method in your controller and dispose that context.
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
db.Dispose();
}
It will be called automatically after the context get completed.
Problem
I've been handed some code, and in the controller class there is a property that holds the initialized database context. ``` public class MyController: Controller { protected AssetManagerContext db = new AssetManagerContext("ConnectionString"); // Actions...etc. [HttpGet] public ActionResult Edit(int id) { MyAsset myAsset = db.Assets.Find(id); // Used and not disposed return View(myAsset); } } ``` Most of the actions use this Context without disposing it, and my concern is that the context is left open. Do I need to be concerned about this context not being explicitly closed (either by .Dispose() or a using {} statement If I should worry, how should I handle this situation, since the variable is part of the class and used across actions?