Entity Framework Caching Issue

2019-01-16 15:50发布

I am new to Entity Framework.

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?

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

Edit:

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();
}

10条回答
▲ chillily
2楼-- · 2019-01-16 16:28

I think you should follow some of the other solutions here, but it seems like you're wanting to clear the cache. You can achieve this by doing the following:

var count = datamodel.Compliances.Local.Count; // number of items in cache (ex. 30)

datamodel.Compliances.Local.ToList().ForEach(c => {
    datamodel.Entry(c).State = EntityState.Detached;
});

count = datamodel.Compliances.Local.Count; // 0
查看更多
Fickle 薄情
3楼-- · 2019-01-16 16:31

When you use EF it by default loads each entity only once per context. The first query creates entity instace and stores it internally. Any subsequent query which requires entity with the same key returns this stored instance. If values in the data store changed you still receive the entity with values from the initial query

A careful answer:

https://stackoverflow.com/a/3653392/1863179

查看更多
乱世女痞
4楼-- · 2019-01-16 16:33

Couple things you can do.

  1. Use a new context. The cached entities are stored in the context. Using a new context prevents it from using the cache.
  2. If you really want a global/long lasting context, you have two sub options: a.) always call the Reload method. db.Entry(entity).Reload() ... this forces the context to reload that entity. b.) use a SqlDependency object to detect when records change and reload the entities as needed. https://code.msdn.microsoft.com/How-to-use-SqlDependency-5c0da0b3
查看更多
萌系小妹纸
5楼-- · 2019-01-16 16:35

I think what you need is GetDatabaseValues(). It is used like:

context.Entry(/*your entry*/).GetDatabaseValues();

Information below is from msdn:

The current values are the values that the properties of the entity currently contain. The original values are the values that were read from the database when the entity was queried. The database values are the values as they are currently stored in the database. Getting the database values is useful when the values in the database may have changed since the entity was queried such as when a concurrent edit to the database has been made by another user.

查看更多
登录 后发表回答