Until now, this was exactly the model that I used, and everything went fine. DbContext should exist as small as possible and should represent the database context (get it?) For the appropriate actions that you use. This is almost the same as having a new controller instance for each request in ASP.NET MVC.
After creating the first instance, subsequent ones quickly accelerate.
In fact, trying to use DbContext more than it would be natural (possibly caching), you get a lot of trouble. This is because of his tracking of fortune, which tends to grow as a charming prince. In addition, performing direct updates or deletions by attaching objects in the old context, when an object with such a primary key was already loaded earlier (possibly in some kind of operation not related to it), throws an exception.
Bottom line: he did not name anything DbCONTEXT. Use it soon, and then remove it.
EDIT: This post only applies to Entity Framework. I am not familiar with NHibernate.
source share