Entity Framework - closing object contexts

After using EFProfiler (absolutely fantastic BTW! Tool) to profile some of our Entity Framework applications, it seems that in most cases all object contexts are not closed.

For example, after it started locally, EF Profiler informed me that there is 326 Object Context, but only 1 was closed?

So my question is: should I worry about this? Or is it self-sufficient inside the Entity Framework?

+4
source share
3 answers

If you are not using an IoC container, can you still close the ObjectContext manually after each request, for example, at the end of your Global.asax request, thereby simulating a β€œfor every request” lifestyle for your contexts?

+4
source

ObjectContexts will eventually be deleted if your application does not explicitly hold onto them, but in general you should try to destroy them deterministically as soon as possible after you are done with them. In most cases, they will connect to the database connections until they are deleted. In my current web application, we use the IoC container (Autofac) to ensure that any ObjectContext object opened at the time of the request is located at the end of the request and does not have to wait for garbage collection.

+2
source

I suggest you make this worry and try to fix the problem, as context contexts are rather cumbersome. If you have too many of them, your application may end up using more memory than necessary, and IIS will restart your application more often than then ...

+2
source

Source: https://habr.com/ru/post/1437822/


All Articles