实体框架缓存问题

问题描述:

我是Entity Framework的新手。

I am new to Entity Framework.

我已经使用EF获取了我的数据库中的某些值。它完美返回,值显示在标签中。但是当我删除表中的所有值(不使用EF)时,EF查询返回我的旧值。我知道EF将值存储在缓存中,并返回缓存的数据以供后续运行。它是否正确?

I have get to some values in my database using EF. It returns perfectly, and the values are shown in labels. But When I delete all values in my table (without using EF), the EF query is returning my old values. I know the EF stores the values in cache and returns the cached data for subsequent runs. Is this correct?

那么当我删除了我的数据库中的所有值时,如何解决问题,但EF返回旧的值?

So how can I solve the problem when I have deleted all values in my database, but EF returns the old values?

编辑

现在我用 datamodel.SaveChanges()。但是现在它返回的旧值是相同的。

Now i used datamodel.SaveChanges(). But now it's return same old values.

我的示例查询如下所示:

My sample query is look like below:

SchoolBriefcaseEntities datamodel = new SchoolBriefcaseEntities();
datamodel.SaveChanges();
List<Compliance> compliance=new List<Compliance>();
IList<ComplianceModel> complianceModel;
if (HttpContext.Current.User.IsInRole("SuperAdmin"))
{
    compliance = datamodel.Compliances.Where(c => c.School.DistrictId == districtId).ToList();
}


如果您知道更改发生在EF之外,想要刷新特定实体的ctxt,可以调用 ObjectContext.Refresh

If you know that changes happened outside of EF and want to refresh your ctxt for a specific entity, you can call ObjectContext.Refresh

datamodel.Refresh(RefreshMode.StoreWins, orders);

如果这似乎是常见的情况,您应该在查询中禁用对象缓存: / p>

If this seems like it will be a common occurance, you should disable object caching in your queries:

SchoolBriefcaseEntities datamodel = new SchoolBriefcaseEntities();
datamodel.tblCities.MergeOption = MergeOption.NoTracking; 

或关闭特定实体的对象级别缓存,

or for to turn off object level caching for specific Entity,

Context.Set<Compliances>().AsNoTracking();