Skip to content
Go to file

Second Level Cache for Entity Framework 6.1

Entity Framework does not currently support caching of query results. A sample EF Caching provider is available for Entity Framework version 5 and earlier but due to changes to the provider model this sample provider does not work with Entity Framework 6 and newer. This project is filling the gap by enabling caching of query results for Entity Framework 6.1 applications.

This project was moved from

You may still find some useful information there:

How to get it

You can get it from NuGet - just install the EntityFramework.Cache NuGet package

How to use it

The project uses a combination of a wrapping provider and a transaction interceptor. A simple InMemoryCache is included in the project. To use it you need first configure EF using code based configuration. Here is an example of how such a configuration looks like.

public class Configuration : DbConfiguration
  public Configuration()
    var transactionHandler = new CacheTransactionHandler(new InMemoryCache());


    var cachingPolicy = new CachingPolicy();

    Loaded +=
      (sender, args) => args.ReplaceService<DbProviderServices>(
        (s, _) => new CachingProviderServices(s, transactionHandler, 

Starting with version 1.1.1 you can also use the new static EntityFrameworkCache.Initialize() method to configure EF to use EFCache. The Initialize method should be invoked at app startup (before EF is used) - e.g. in the application static constructor. To initialize EFCache with the built-in InMemoryCache you can use the following code:

EntityFrameworkCache.Initialize(new InMemoryCache());

You can find more details in my blogpost


I am providing code in the repository to you under an open source license. Because this is my personal repository, the license you receive to my code is from me and not my employer (Facebook)

You can’t perform that action at this time.