Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObjectContext.Refresh()?

How to update ALL the dirty entities from the data store, and reset their changed values to the original store value?

The method ObjectContext.Refresh requires as a parameter the entities to be refreshed.

like image 663
Shimmy Weitzhandler Avatar asked Nov 17 '09 06:11

Shimmy Weitzhandler


2 Answers

The following usually works:

Context.Refresh(RefreshMode.StoreWins, _
    Context.ObjectStateManager.GetObjectStateEntries())

It sometimes causes problems with EntityRelations. look at my comment for further details.

like image 164
Shimmy Weitzhandler Avatar answered Sep 19 '22 06:09

Shimmy Weitzhandler


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.Deleted 
                                               | EntityState.Modified 
                                               | EntityState.Unchanged)
                                      where entry.EntityKey != null
                                      select entry.Entity);

     context.Refresh(RefreshMode.StoreWins, refreshableObjects);
}

I wrote a post on how to RefreshAll() and refresh the context in some other ways:

http://christianarg.wordpress.com/2013/06/13/entityframework-refreshall-loaded-entities-from-database/

like image 11
Christian Rodriguez Avatar answered Sep 19 '22 06:09

Christian Rodriguez