Refresh in Entity Framework

Entity Framework by default caches the objects retured from the database.
This is convenient, for example, for pseudo-static reference tables, that rarely change and you can accept a restart of your application.

But more times you don't want this, this for example to data updated by multiple front-ends!
So, how can you disable Entity Framework caching?

The solution is:

var query = "some entity framework query"
Context.Refresh(RefreshMode.StoreWins, query);
IList<myEntity> myList = myQuery.ToList();

Having called the Context.Refresh(...) static method before ToList(), makes that the latter one handles correctly inserted and deleted entities, other than updated ones.

You can find a bit more details in the following question+answers page: How to refresh ObjectContext cache from db?.

Add comment

Loading