ObjectContext.Refresh()?

ぐ巨炮叔叔 提交于 2019-11-27 04:16:23

The following usually works:

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

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

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/

If you want to reset ALL the changes, you could set the ObjectContext to null and re-instantiate it.

I believe this will achieve what you want.

Kindness,

Dan

GBrian

We use this:

return Context.ObjectStateManager.GetObjectStateEntries(System.Data.EntityState.Deleted
System.Data.EntityState.Modified).All(ose 
  => {
    if(ose.Entity != null)
      Context.Refresh(RefreshMode.StoreWins, ose.Entity);
      return true;
    });

Where "Context" is the context to refresh. We filter by change state and entities to avoid new entities and relations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!