Programatially Clear Cache For A Specific Table(s)?

Jan 9, 2016 at 6:46 AM
I am manually doing some batch changes to a table, including truncate and then batch inserts. After I have completed this, I have the problem where the cache is still active.

Is there a method I can call to pass in the Table name(s) to clear/invalidate the cache for that table?
Jan 13, 2016 at 5:40 PM
You can clear cache for given tables using the ICache.InvalidateSets method. It takes an enumerable of store EntitySet names. The mapping between a table in the database and a store EntitySet is 1:1 so it's relatively easy to get entity set names if you have table names. Take a look at this thread - towards the end it shows how to get all the entity sets. You just need to filter out ones you don't want. If you are not using CodeFirst the store container names will not be "CodeFirstDatabase" - you need to use the name from your EDMX or just take the first one since EF does not support multiple entity containers.

Apr 18, 2016 at 1:19 PM
Thanks for the reply on this, but I cannot figure out 'how' I get to the method InvalidateSets without instantiating it in my controller like so
internal static readonly InMemoryCache Cache = new InMemoryCache();
But that is then a different instance, and doesn't contain the sets I want to invalidate? I am using this code to set up your caching.
public class CachingConfiguration : DbConfiguration
    public CachingConfiguration()
        var transactionHandler = new CacheTransactionHandler(new InMemoryCache());


        var cachingPolicy = new CachingPolicy();

        Loaded +=
            (sender, args) => args.ReplaceService<DbProviderServices>(
            (s, _) => new CachingProviderServices(s, transactionHandler,
How would I now get hold of that method now? From the DbContext somehow?
Apr 19, 2016 at 5:45 AM
You already have a static property containing the cache. Why can't you pass it when creating a new instance of the CacheTransactionHandler? I.e. instead of:
var transactionHandler = new CacheTransactionHandler(new InMemoryCache());
var transactionHandler = new CacheTransactionHandler({your type}.Cache);
Apr 19, 2016 at 5:51 AM
Yeah sorry. Got what you were saying in the end. I just created a static class
public static class MemoryCacheResolver
    internal static readonly InMemoryCache Cache = new InMemoryCache();
Passed that into the CacheTransactionHandler in the CachingConfiguration Class and use it like so
Apr 19, 2016 at 2:25 PM
Yes. This is what I meant. It's not ideal but probably the simplest. There is no way to retrieve transaction handler once it is registered.