Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caching Entity Framework DbContexts per request

I have several classes based on System.Entity.Data.DbContext. They get used several times a request in disparate ends of the web application - is it expensive to instantiate them?

I was caching a copy of them in HttpContext.Current.Items because it didn't feel right to have several copies of them per request, but I have now found out that it doesn't get automatically disposed from the HttpContext at the end of the request. Before I set out writing the code to dispose it (in Application_EndRequest), I thought I'd readdress the situation as there really is no point caching them if I should just instantiate them where I need them and dispose them there and then.

Questions similar to this have been asked around the internet, but I can't seem to find one that answers my question exactly. Sorry if I'm repeating someone though.

Update

I've found out that disposing of the contexts probably doesn't matter in this blog post, but I'm still interested to hear whether they are expensive to instantiate in the first place. Basically, is there lots of EF magic going on there behind the scenes that I want to avoid doing too often?

like image 613
sjmeverett Avatar asked Jul 05 '11 09:07

sjmeverett


People also ask

Does DbContext cache?

Object CachingThe DbContext provides a first-level cache for objects that it is asked to retrieve from the data store. Subsequent requests for the same object will return the cached object instead of executing another database request.

Does EF core cache data by default?

The Cache: The memory cache is used by default. The Cache Key: The cache key is created by combining a cache prefix, all cache tags and the query expression. The Query Materialized: The query is materialized by either using "ToList()" method or "Execute()" method for query deferred.

Does Entity Framework cache data?

Entity Framework has the following forms of caching built-in: Object caching – the ObjectStateManager built into an ObjectContext instance keeps track in memory of the objects that have been retrieved using that instance. This is also known as first-level cache.

Which caching does Entity Framework support?

Caching with Entity Framework NCache introduces the caching provider which acts between Entity Framework and the Data source. The major reason behind the EF Caching provider is to reduce database trips (which slow down application performance) and serve the query result from the cache.


2 Answers

Best bet would be to use an IoC container to manage lifecycles here -- they are very, very good at it and this is quite a common scenario. Has the added advantage of making dynamic invocation easy -- meaning requests for your stylesheet won't create a DB context because it is hardcoded in BeginRequest().

like image 174
Wyatt Barnett Avatar answered Sep 21 '22 22:09

Wyatt Barnett


I'm answering my own question for completeness.

This answer provides more information about this issue.

In summary, it isn't that expensive to instantiate the DbContext, so don't worry.

Furthermore, you don't really need to worry about disposing the data contexts either. You might notice ScottGu doesn't in his samples (he usually has the context as a private field on the controller). This answer has some good information from the Linq to SQL team about disposing data contexts, and this blog post also expands on the subject.

like image 41
sjmeverett Avatar answered Sep 20 '22 22:09

sjmeverett