ObjectContext.Refresh()?
Asked Answered
S

4

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 parameter the entities to be refreshed.

Straiten answered 17/11, 2009 at 6:18 Comment(0)
S
16

The following usually works:

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

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

Straiten answered 17/11, 2009 at 6:39 Comment(4)
This just saved me many lines of code walking the object graph myself.Galer
There is a bug, please take a look: #1758372Straiten
Sometimes you'd like to update even the unchanged items, cuz you want to refresh them against the changes made in database by other clients.Straiten
@Shimmy GetObjectStateEntries() requires a parameter (e.g. Modified), and when I pass one in I get an exception that the entity is disconnected from the context. Any ideas?Moskva
S
11

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/

Smoothen answered 13/6, 2013 at 16:27 Comment(3)
This will throw an exception if you have added items. Add this before to fix: var addedEntries = context.ObjectStateManager.GetObjectStateEntries(EntityState.Added).ToList(); addedEntries.ForEach(entry => entry.Delete());Mohammadmohammed
@Mohammadmohammed You are right about the exception. Actually if you think about is it doesn't make sense to try to refresh a just added entity. I've edited the response removing the EntityState.Added from the code. In your proposed solution you are actually deleting the just added entity, so nothing will be added.Smoothen
Is this related to SQL Version, so it works when running on a 12.0 SQL server but 11.0 ?Delarosa
D
0

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

Dulla answered 17/11, 2009 at 6:27 Comment(1)
I guess you work with ASP.NET, so it's easy for you to say it, I use a long life-cycle context instance.Straiten
F
0

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.

Floats answered 1/12, 2010 at 10:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.