How to cache certain Entity Framework instances

.net entity entity-framework


Use of Entity Framework is mine.

My database contains information on people and the country of their birth. I want to know where each person is from while I'm loading a person object. Is there a method to cache the list of nations so that extra queries are avoided?

7/18/2009 6:41:28 PM

Accepted Answer

My friend Jarek has a great example Caching provider for the Entity Framework, as Luhmann has mentioned. wherein he discusses zzz-5 zzz.

However, there are situations when this strategy is excessive, and I believe that caching nations is an excellent example of a situation when the effort isn't worthwhile. Implementing some kind of ad-hoc caching could be preferable.

The main issue with attempting to do this is that an instance Entity Framework entity can only be attached to one ObjectContext at a time, which means that if you simply grab items from the cache and use them blindly when you are receiving concurrent requests (like in a website), you will run into issues.

What you need is a cloning cache, which copies items when you take them out of the cache.

I created a guide that demonstrates how to do this How to cache Entity Framework reference data is covered in Tip 14..

Hope this is useful.

James Alex

Microsoft Entity Framework Team.

7/18/2009 6:34:36 PM

Popular Answer

You should check out Jaroslaw Kowalski's superb Entity Framework provider wrappers for tracing and caching.

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow