Second Level Caching with Entity Framework
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.
2nd Level caching with a distributed cache
by
Wes Nur
Educational Content
Writing Usable APIs in Practice
Giovanni Asproni May 19, 2013




Hello stranger!
You need to Register an InfoQ account 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