How to force Entity Framework to always get updated data from the database?

Saravana picture Saravana · Mar 4, 2014 · Viewed 83k times · Source

I am using EntityFramework.Extended library to perform batch updates. The only problem is EF does not keep track of the batch updates performed by the library. So when I query the DbContext again it does not return the updated entities.

I found that using AsNoTracking() method while querying disables the tracking and gets fresh data from the database. However, since EF does not keep track of the entities queried with AsNoTracking(), I am not able to perform any update on the queried data.

Is there any way to force EF to get the latest data while tracking changes?

Answer

PlTaylor picture PlTaylor · Mar 4, 2014

Please try this to refresh a single entity:

Context.Entry<T>(entity).Reload()

Edit: To get fresh data for a collection of entities is worth trying to dispose the DbContext instance after each request.