ObjectContext.Refresh()?

后端 未结 4 2007
天涯浪人
天涯浪人 2020-11-30 06:15

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

4条回答
  •  旧巷少年郎
    2020-11-30 06:42

    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/

提交回复
热议问题