Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 451
MR.ABC Avatar asked Mar 22 '13 12:03

MR.ABC


1 Answers

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.

like image 166
Paul Zahra Avatar answered Nov 10 '22 03:11

Paul Zahra