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();
}
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,
I suspect that the underlying problem here is that your
DbContext
is hanging around too long. I can tell from the fact that you are usingHttpContext
that you have a web application, and General guidelines when working with DbContext includeIf you are using MVC you could use the Dispose pattern in your controller like this:
But you really ought to be looking at dependency injection to manage the DbContext lifetime
Below code helped my object to be refreshed with fresh database values. The Entry(object).Reload() command forces the object to recall database values
Firstly I would not suggest modifying the database external to your system unless you are only doing testing and development.
The EF DbContext contains an IDisposable interface. To release any cached data either make the Dispose calls manually or place your Database Object inside a using block.
This will make sure the context is cleared and recreated the next time it is used. Make sure to do this for all your calls and not just the one you are having issues with.
If you know that changes happened outside of EF and want to refresh your ctxt for a specific entity, you can call ObjectContext.Refresh
If this seems like it will be a common occurance, you should disable object caching in your queries:
or for to turn off object level caching for specific Entity,
I recommend you to use some MergeOption to all EntitieSet after create the context, like this:
Read about the MergeOption here: http://msdn.microsoft.com/en-us/library/system.data.objects.mergeoption.aspx Your will use NoTracking, I think.
But, iy you whant CLEAR the "cached" entities, detaching it.
Where Ctx are my Context.