Should Entity Framework Context be Put into Using Statement?

If you create a context, you must dispose it later. If you should use the using statement depends on the life time of the context.

  1. If you create the context in a method and use it only within this method, you should really use the using statement because it gives you the exception handling without any additional code.

  2. If you use the context for a longer period – that is the life time is not bound by the execution time of a method – you cannot use the using statement and you have to call Dispose() yourself and take care that you always call it.

What does Dispose()do for an object context?

I did not look at the code, but at least I exspect it to close the database connection with its underlying sockets or whatever resources the transport mechanism used.

Leave a Comment