How to refresh ObjectContext cache from db?

LukLed picture LukLed · Feb 25, 2010 · Viewed 21.5k times · Source

We are loading data from db:

var somethings = Context.SomethingSet.ToList();

Then someone deletes or adds rows outside of context. Out context still has caches deleted object, because it doesn't know they were deleted. Even if I call Context.SomethingSet.ToList(), our context still contains deleted objects and navigation properties are not correct.

What is the best method to refresh whole set from database?

Answer

Josh picture Josh · Feb 25, 2010

The Refresh method is what you are looking for:

Context.Refresh(RefreshMode.StoreWins, somethings);