How to force refresh all entities in Entity Framework 4.0
I'm wondering if it is possible to refresh开发者_运维技巧 all entities from data model as opposite to refresh them one by one.
Something like entities.RefreshAll();
Yes you can use this code:
public void RefreshAll()
{
// Get all objects in statemanager with entityKey
// (context.Refresh will throw an exception otherwise)
var refreshableObjects = (from entry in context.ObjectStateManager.GetObjectStateEntries(
EntityState.Added
| EntityState.Deleted
| EntityState.Modified
| EntityState.Unchanged)
where entry.EntityKey != null
select entry.Entity);
context.Refresh(RefreshMode.StoreWins, refreshableObjects);
}
I wrote another couple of ways of refreshing with EF:
http://christianarg.wordpress.com/2013/06/13/entityframework-refreshall-loaded-entities-from-database/
精彩评论