Entity Framework Caching Issue
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
EF will not load changes unless you re query the context. EF queries db and loads maps them into objects, it watches changes you perform on objects and not on the database. EF does not track changes made directly to database and it will never track.
You have loaded a List, that List is your cache in memory. Even calling Save Changes will not refresh. You will have to query the context once again, that is create new list.
To see changes You will have to execute following line once more,
datamodel.Compliances.Where(c => c.School.DistrictId == districtId).ToList()
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,
Context.Set<Compliances>().AsNoTracking();