Entity Framework Caching Issue

asp.net-mvc c# catch-all entity-framework


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?


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

My sample query is look like below:

SchoolBriefcaseEntities datamodel = new SchoolBriefcaseEntities();
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();
1/29/2019 8:34:30 AM

Accepted Answer

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

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,

5/11/2015 2:29:09 PM

Popular Answer

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:


Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow