1

I always have seen lots of questions about how to handle the life-cycle of an EF context, but never found a concrete answer to this.

As stated everywhere, context is intended to be used as a unit work and be disposed whenever you finish that work.

So, let's suppose in a program we create a class to manage all the tipical database tasks (create user, update user, delete user, etc..) and in each one we create a context wrapped into a using statement as is intended to be used (at least on all info I have found).

So, now, in our main program in a function we use, let's say, 3 or 4 of those functions. Does that mean we have opened and closed four connections to the database or does EF uses a pooling mechanism to reuse the same connection?

Connecting to the DB is a very consuming process (compared to execurte simple queries) and when using manually connections I tend to pool them to reuse, but with EF I am lost, don't know if I should pool contexts, pool connections and create contexts using that connections or do nothing as the EF will take care of it.

3
  • 1
    social.msdn.microsoft.com/Forums/en-US/… Commented May 30, 2014 at 8:53
  • one more option you have is leverage dependency injector to implement container per request pattern. Basically you can create a nested container to handle the life cycle of context object. You can associate the life cycle of dbcontext object with the nested container and when a httprequest is finished, destroy the nested container explicitly, the dbcontext associated will be destroyed. Commented May 30, 2014 at 10:23
  • First of all, im not asking for asp .net, its for a windows service, so it does not make se es, and also, if that were the case, in asp is really easy to create the Context in the page load and unload, but that can be very bogus because the Context can enter into an invalid state, you should create a new one, and if you suppose that Context will remaining the same for all the page life it will cause a lot of troubles. Commented May 30, 2014 at 10:29

1 Answer 1

2

If all your EF instances share the same connection string, then by default it uses a connection pool. However, I would recommend you to read about the Unit of Work pattern

http://www.asp.net/mvc/tutorials/getting-started-with-ef-5-using-mvc-4/implementing-the-repository-and-unit-of-work-patterns-in-an-asp-net-mvc-application

http://www.codeproject.com/Articles/615499/Models-POCO-Entity-Framework-and-Data-Patterns

Sign up to request clarification or add additional context in comments.

3 Comments

THANK YOU!!! that's the part of the info I did not found anywhere, so, it's safe to destroy as many contexts you need because they will reuse connections (if they can), correct?
In fact you must. The data context should be used as a Unit Of Work per se. However, the pattern can be extended as in the previous posted links.
Thanks again, I always have been worried about this and found nowhere this concrete answer, now i have peace of mind :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.