1

在过去,我有这个 http://arens.ws/wordpress/?p=54

public static void ClearCache(string entityName)
{
  const string format = "adxdependency:crm:entity:{0}";
  var dependency = string.Format(format, entityName).ToLower();

  var cache = Microsoft.Xrm.Client.Caching.CacheManager.GetBaseCache();
  cache.Remove(dependency);
}

2011年就不同了。有任何想法吗?

4

1 回答 1

1

It has changed only a little bit:

ObjectCache cache = Microsoft.Xrm.Client.Caching.ObjectCacheManager
    .GetInstance("Xrm");
string cachekey = String.Format("xrm:dependency:entity:{0}:id={1:D}", 
    entity.LogicalName, entity.Id);
cache.Remove(cachekey);

I found no documentation for this, found the key naming scheme by enumerating the cache. Probably using this is not a best practice, it could change again in the next version? There should be a better way to do this...

[Update] There is a better way.

Try this:

var serviceContext = (Get an OrganizationServiceContext);

var serviceContainer = serviceContext as 
                           OrganizationServiceContainer;
var cachedOrgService = serviceContainer.Service as 
                           CachedOrganizationService;
var orgServiceCache = cachedOrgService.Cache as 
                           IOrganizationServiceCache;
var entity = (Get the entity that was updated);

orgServiceCache.Remove(entity.LogicalName, entity.Id);

Works like a charm...

于 2012-04-04T00:52:43.933 回答