MR.ABC
MR.ABC

Reputation: 4862

Entity Framework 5 InvalidOperationException on Reload

I try to discard some changes with the reload function. I get an InvalidOperationException. How can i prevent this ?

DbContext.SaveChanges();
//Entity is in Unchanged state
//Make some changes to an entity
//Change state to modified
DbContext.Entry(entity).Reload();

InvalidOperationException
EntityMemberChanged or EntityComplexMemberChanged was called without first calling EntityMemberChanging or EntityComplexMemberChanging on the same change tracker with the same property name. For information about properly reporting changes, see the Entity Framework documentation.

EDIT:
I've enabled and disabled ProxyCreationEnabled, LazyLoadingEnabled.
Tried different approaches as well. All these attempts throw the same exception.

var objContext = ((IObjectContextAdapter)context).ObjectContext;
objContext.Refresh(RefreshMode.ClientWins, entry.Entity);


entry.OriginalValues.SetValues(entry.GetDatabaseValues());

Hope i get a solution. Don't want to dispose the full DbContext to reload all the data.

Upvotes: 6

Views: 2167

Answers (4)

Hannish
Hannish

Reputation: 1532

I had a similar situation, with the same exception description. In my case I was trying to delete an entity from the context, and it was somehow related to a propertychanged handler still being called.

I just removed the handler before removing the entity from the context with

MyEntity.PropertyChanged -= MyPropertyChangedHandler;
context.MySet.Remove(MyEntity); //it works after removing the handler

Hope this helps someone.

Upvotes: 0

Matstar
Matstar

Reputation: 410

I found that the reload fails on proxy entities that have navigation properties.

As a work around, reset the current values and then reload like this:

var entry = DbContext.Entry(entity);
entry.CurrentValues.SetValues(entry.OriginalValues); 
entry.Reload();

Upvotes: 0

Chris Moschini
Chris Moschini

Reputation: 37947

If the code is as you posted it, where the object is loaded from a DbContext, then reloaded from that same DbContext, you should not be explicitly marking it as Modified; making changes to the Entity is enough to mark it as Modified already. In other words:

var o = new SimpleObject { Stuff = "One" };
db.SimpleObjects.Add(o);
db.SaveChanges();

o.Stuff = "Two"; // implicitly marks as Modified for you, since it's still Attached

// Unnecessary
//db.Entry(o).State = System.Data.EntityState.Modified;

db.Entry(o).Reload(); // Works for me

Upvotes: 0

BLoB
BLoB

Reputation: 9725

To quote this MSDN thread / post

"it's worth noting that the error shows up whether or not you use change tracking a via a proxy class or call entitymemberchanged explicitly. I seem to get the error whenever I execute entitymemberchanging and changed on a a thread outside the one that created the objectcontext/objectstatemanager, regardless of whether i execute the two functions synchronously or asynchronously, use locks or have the thread explicitly sleep. It occurs to me that this is some sort of "genuine bug" with the objectstatemanager, and not something for which there would be a simple workaround. Ball's in your court, MSFT."

P.S. Too long for comment.

Upvotes: 2

Related Questions