Good practice using a different DbContext class for each page?

I am optimizing and refactoring a large ASP.NET ERP application to achieve a faster development experience. We currently have a large model (600+ tables / entities) that is created when the application starts, which takes about 15 seconds. (At this point we are using NHibernate with code mappings)

I am trying to do a quick page rendering after compilation.

I was wondering if it would be good practice to have one DbContext per page that will include only the necessary entities / mappings for using this page. Each page is considered as a separate element and can be transported by itself.

EDIT: I'm not talking about reusing an instance of DbContext, but another object with different DbSets. A different instance will be created for each request.

+6
source share
4 answers

I was wondering if it would be good practice to have one DbContext per page that will include only the required entities / mappings for use this page.

I don’t think it’s a good idea to have one highlighted DbContext type per page.

  • Each model of type DbContext will hold the amount of memory during the entire life cycle of the application. I would say that the trace will be approximately 5 MB for approximately 20 types of entities.
  • It will be difficult for you to determine the boundaries of each of your DbContext types (ignoring the navigation properties of your entities in each case, you will force you to define many highlighted matching configurations). Not that this is a bad idea, but I believe that supporting Linq would be almost useless, and I think micro-orm is better suited for this approach.

You can also use a limited context: divide your domain model into groups of objects corresponding to the same problem (for example, no more than 50 objects for each problem) and use a special DbContext type for each problem (without navigation properties for different DbContexts objects). Note that you can define very simple crosstalk (mostly read-only) to use crosstalk concepts for your entire application (for example, a UserSummary object that displays the basic properties of a User object)

Regarding speeding up the launch, I think this can help. 3 STEPS FOR FAST ENTITYFRAMEWORK 6.1 FIRST START-UP CODING

+5
source

It is recommended that you use a DbContext for each request.

If you display partial pages in your view better, if you are still using the same context.

Your context is automatically placed at the end of your request as a whole (Of course, there are some exceptions if you create your context when the application starts, for example, but you should not do this), but it is better to delete it manually at the end of the request.

But if you are talking about the best performance, I was at a conference where the StackOverflow team talks about how SO works (it is also written in MVC).

They say that they do not use EF or any other ORM at all, because it creates many intermediate objects, and GarbageCollector cannot dispose of it quickly enough, so they write everything in the good old plain ADO.NET

+3
source

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.

0
source

Entity Framework 6 added support for using multiple models in one database, including migration. But each of the models must be independent of other models, that is, there are no common tables and entities. Entity sharing can be done, but migrations become more complex.

This blog post Data Points - The First Steps of EF6 for Multiple Models , explains in detail what is supported in EF6, and also has links to other presentations about sharing objects on different models.

If you mainly focus on the amount of time spent during development, you can create separate DbContexts for each page or group of pages that do not have migration (and therefore, there should be no problems with shared objects between DbContexts). Then you have one DbContext that you use in a production that contains all entities and has migration. Give them all the interfaces and bind them along with dependency injection, and then you can easily switch between DbContexts for development, testing and production.

0
source

All Articles