C#代码:
刚开始利用QueryExpression,在Gridview绑定数据源的时候,不用手动清除上下文中存在的Entity;
但是在动态生成Table的时候,我们发现相同的QueryExpression会查出还是以前某一次的查询结果,而不是数据库的最新结果,所以我们要清除一下当前的缓存,方法如下:
Var context = new XrmServiceContext();
EntityCollection entityCollection = context.RetrieveMultiple(query);
C# Code
//删除当前的缓存
Microsoft.Xrm.Client.Caching.ObjectCacheManager.Clear(Microsoft.Xrm.Client.Caching.ObjectCacheManager.GetInstance());
或者
C# Code
// 删除当前的缓存
Microsoft.Xrm.Client.Caching.ObjectCacheManager.RemoveAll(Microsoft.Xrm.Client.Caching.ObjectCacheManager.GetInstance());
或者
C# Code
//删除当前的缓存
protected void ClearAllCRMCache(string entityName)
{
var dependency = string.Format("adxdependency:crm:entity:{0}", entityName).ToLower();
var cache = Microsoft.Xrm.Client.Caching.ObjectCacheManager.GetInstance();
cache.RemoveAll();
}

本文介绍了解决使用QueryExpression查询时返回旧数据的问题。通过清除CRM缓存,确保每次查询都能获取到最新的数据库结果。

被折叠的 条评论
为什么被折叠?



