Second Level Caching with Entity Framework

| by Roopesh Shenoy Follow 0 Followers on Sep 07, 2011. Estimated reading time: 1 minute |

Second Level Caching is one of the features that is not present out-of-the-box in Entity Framework. In the MSDN article “Second-Level Caching in the Entity Framework and AppFabric” Julie Lerman shows how to implement Second Level Caching with Entity Framework to take advantage of caching services like Microsoft AppFabric.

Why do we need Second Level Caching?

One of the great benefits of the EF is its ability to automatically generate database persistence commands (inserts, updates and deletes) based on the state information found in the context. But if that state information is unavailable, the EF has nothing to do when it’s time to call SaveChanges.

Second-level caches are instrumental in solving this type of problem. These caches exist outside of the transaction—often outside of the application—and therefore are available to any context instance.

Julie extends the EF Caching Provider designed by Jarek Kowalski to make it work with Windows Server AppFabric. She modifies the adapter class to make it work with AppFabric client API, and extends the ObjectContext class. You can download the code sample from the article to learn more.

Second level caching is also useful for improving performance of highly transactional applications by caching commonly queried data and avoiding frequently hitting the database. Microsoft AppFabric is not the only caching solution that needs to be used with ORMs like Entity framework – other frameworks like memcached can also be used for the same purpose with appropriate changes to the code.

ADO.NET Entity Framework is an Object Relational Mapper from Microsoft. EF 4.1 was recently released with interesting features such as Validation, Spatial data, Enums and more. 


Rate this Article

Adoption Stage

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

2nd Level caching with a distributed cache by Wes Nur

Basically second level cache is used to share load with database, for huge databases you do need to have second level cache in order to enhance performance and remove bottlenecks. Moreover if your .NET application is running in a server farm then your cache must be distributed in order to avoid data integrity problems in your cache. I’d suggest you to have a look at NCache as it solves the issues like data integrity, scalability, data consistency etc.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

1 Discuss

Login to InfoQ to interact with what matters most to you.

Recover your password...


Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.


More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.


Stay up-to-date

Set up your notifications and don't miss out on content that matters to you