Daniel Skowroński
Daniel Skowroński

Reputation: 411

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();

Upvotes: 1

Views: 2725

Answers (1)

Christian Rodriguez
Christian Rodriguez

Reputation: 964

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/

Upvotes: 2

Related Questions