Initialization of the object-object of the contextcontext object with each call

I have a web application that uses the Entity framework as a data access layer. Now I initialize an entity class that inherits an ObjectContext for each request.

I just want to know if there are any flaws or consequences in terms of the performance of this. Is it better to cache this object.

Please note that I have large edmx files, some contain about 50 tables

+7
source share
3 answers

Initializing an ObjectContext for each request is probably the most common way to implement EF in a web application. This is not a performance issue; initialization is pretty cheap. ObjectContext is an implementation of the Unit of Work EF template, and therefore it is good practice to encapsulate conversations with the database into a single unit of work. Caching an ObjectContext object in all requests can be problematic, because lengthy database conversations are not easy to process in web applications, because you never know when the next request will come from this client.

+4
source

This is the recommended practice when working with the Entity Framework in web applications.

However, you can split your context containing 50 tables into a couple of contexts if you can split your tables into independent areas. Then it will be much easier for you to manage contexts.

Context caching is not recommended. ObjectContext is not thread safe. IT will also disrupt the work structure. This will lead to undesirable behavior, for example, making changes to multiple users in one transaction.

+2
source

Using a new context instance for a request or action is required . The effect does not affect this, because metadata (from EDMX) is downloaded and compiled only once (the first time you need them), after which metadata is reused for all instances of the same context until the application pool processes it. You can even speed up initialization by pre-compiling metadata .

+2
source

All Articles