Search code examples
.netasp.net-mvcentity-frameworkcachingentity-framework-6

Entity Framework second level cache


I want to use EntityFramework.Cache for one of my tables, but I can not find any information about how does it work with live data and relationships?

I want to know if I add some data to my table or the relation tables does it understand it and re-cache table again or the result could be stale?

Does anyone work with any Entity Framework second level cache?


Solution

  • A cache normally caches entities (Detached from the context) and re-uses the same result as long as the cache is not expired.

    If you change the query, a new result is cached (even if it returns the same entity key) since relations could have been changed.

    Here is an alternative to the cache you are currently using:

    Disclaimer: I'm the owner of the project EntityFramework Plus on GitHub

    EF+ Query Cache allows to cache query with tag, expiration and cache control.

    Documentation: Entity Framework - Second Level Cache

    EDIT: Answer comment

    if data has been updated, and new information must be served up that we can remove specific entities from cache

    For EF6, we have the built-in IsAutoExpireCacheEnabled option:

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        QueryCacheManager.IsAutoExpireCacheEnabled = true;
    }
    

    For EF Core, we didn't find out how to make it happen yet.