6

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.

MR.ABC
  • 4,114
  • 11
  • 38
  • 80
  • 1
    Would any of these help? http://stackoverflow.com/questions/5221314/refresh-entity-instance-with-dbcontext or this http://stackoverflow.com/questions/5799737/entity-framework-4-1-dbset-reload or this http://connect.microsoft.com/VisualStudio/feedback/details/694567/entitymemberchanged-or-entitycomplexmemberchanged-was-called-without-first-calling-entitymemberchanging-or-entitycomplexmemberchanging-on-the-same-change-tracker-with-the-same-property-name ?? – LiverpoolsNumber9 Mar 22 '13 at 12:16
  • This may have nothing to do with your situation, but wanted to check.. Are you doing any type of multithreading/async in your program? Like using any PLINQ or 4.5 async/await? – tostringtheory Mar 25 '13 at 01:26
  • @tostringtheory Allready checked it without any threading. Same problem. – MR.ABC Mar 25 '13 at 05:52
  • This smells of a threading issue or self tracking entities... what's the application type - web, WPF..? – Moho Mar 27 '13 at 00:50
  • Can you include the exact code you're using to change the state to Modified? I've seen some confusion on how to do this in a number of places including here on SO. – Chris Moschini Mar 31 '13 at 19:23

4 Answers4

2

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.

Paul Zahra
  • 8,906
  • 7
  • 49
  • 65
0

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
Chris Moschini
  • 33,398
  • 18
  • 147
  • 176
0

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();
Matstar
  • 362
  • 2
  • 3
0

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.

Hannish
  • 1,412
  • 1
  • 21
  • 32