does MVC automatically dispose of the database context I instantiate in a controller or anywhere else or does it persist?
Do I need to use using or can I not worry about that?
does MVC automatically dispose of the database context I instantiate in a controller or anywhere else or does it persist?
Do I need to use using or can I not worry about that?
If you are talking about an EF data context, the answer is no, ASP.NET MVC, doesn't dispose it automatically but you shouldn't be worried about disposing it as Stephen Walther explains in his blog post. And here's a similar answer.
Whenever initializing an object that is defined as IDisposable
, you should wrap the creation in a using
statement. This is a general good rule to follow and ensures disposal.
This includes your data context. If you don't and the controller throws in the middle of using the data context, you may end up with open connections.