Entity Framework 5 InvalidOperationException on Reload

I am trying to undo some changes using 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 is called without first calling EntityMemberChanging or EntityComplexMemberChanging on the same change tracker with the same property name. For information on proper change reporting, see the Entity Framework documentation.

EDIT:
I turned on and off ProxyCreationEnabled , LazyLoadingEnabled .
We tried other approaches. All these attempts raise the same exception.

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


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

I hope I get a solution. Do not want to delete the full DbContext in order to reload all the data.

+6
source share
4 answers

To quote this MSDN thread / message

"it’s worth noting that the error shows whether you are using change tracking a through a proxy class or calling entitymemberchanged explicitly. It seems that I get an error whenever I execute entitymember and change to aa stream outside the one that created the objectcontext / objectstatemanager, regardless on whether I perform two functions synchronously or asynchronously, use locks or explicitly hide the stream. It seems to me that this is some kind of "real mistake" with the objectstatemanager, and not what it would be a simple workaround for. Ball in your court, MSFT. "

PS Too long for comments.

+2
source

If the code, as you have placed it, where the object is loaded from the DbContext and then reloaded from the same DbContext, you should not explicitly indicate it as Modified; making changes to Entity is enough to mark it as changed. 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 still Attached // Unnecessary //db.Entry(o).State = System.Data.EntityState.Modified; db.Entry(o).Reload(); // Works for me 
0
source

I found that the reboot failed with proxies with navigation properties.

How it works, reset the current values, and then reload like this:

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

I had a similar situation, with the same exception description. In my case, I tried to remove the object from the context, and it is somehow related to the propertychanged handler, which is still being called.

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

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

Hope this helps someone.

0
source

All Articles