0

I have a EF 4 POCO DbContext and am showing a table in a DataGrid (System.Windows.Controls.DataGrid).

  1. I can SaveChanges to store changes made in the DataGrid.
  2. I can Reload() to get data updated by another user (ie me using SQL Management Studio).
  3. I can then see the other user's data

However, if I try to do 1 then 2 (i.e. make a change in the grid, SaveChanges, after that another user changes the data then I Reload) I get an InvalidOperationException.

This happens only when changing the text through the grid, not when changing the data through code.

I can quietly swallow the exception and then see the updated data. However I'd rather not do that without understanding what's going on...

What's going on?

The complete exception is...

System.InvalidOperationException occurred HResult=-2146233079 Message=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. Source=System.Data.Entity StackTrace: at System.Data.Objects.EntityEntry.EntityMemberChanged(String entityMemberName, Object complexObject, String complexObjectMemberName) at System.Data.Objects.EntityEntry.EntityMemberChanged(String entityMemberName) at System.Data.Objects.ObjectStateEntry.System.Data.Objects.DataClasses.IEntityChangeTracker.EntityMemberChanged(String entityMemberName) at System.Data.Objects.Internal.SnapshotChangeTrackingStrategy.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value) at System.Data.Objects.Internal.EntityWrapper1.SetCurrentValue(EntityEntry entry, StateManagerMemberMetadata member, Int32 ordinal, Object target, Object value) at System.Data.Objects.EntityEntry.SetCurrentEntityValue(StateManagerTypeMetadata metadata, Int32 ordinal, Object userObject, Object newValue) at System.Data.Objects.ObjectStateEntryDbUpdatableDataRecord.SetRecordValue(Int32 ordinal, Object value) at System.Data.Objects.EntityEntry.UpdateRecord(Object value, DbUpdatableDataRecord current, UpdateRecordBehavior behavior, Int32 propertyIndex) at System.Data.Objects.Internal.SnapshotChangeTrackingStrategy.UpdateCurrentValueRecord(Object value, EntityEntry entry) at System.Data.Objects.Internal.EntityWrapper1.UpdateCurrentValueRecord(Object value, EntityEntry entry) at System.Data.Common.Internal.Materialization.Shaper.UpdateEntry[TEntity](IEntityWrapper wrappedEntity, EntityEntry existingEntry) at System.Data.Common.Internal.Materialization.Shaper.HandleEntity[TEntity](IEntityWrapper wrappedEntity, EntityKey entityKey, EntitySet entitySet) at lambda_method(Closure , Shaper ) at System.Data.Common.Internal.Materialization.Coordinator1.ReadNextElement(Shaper shaper) at System.Data.Common.Internal.Materialization.Shaper1.SimpleEnumerator.MoveNext() at System.Data.Objects.ObjectContext.BatchRefreshEntitiesByKey(RefreshMode refreshMode, Dictionary2 trackedEntities, EntitySet targetSet, List1 targetKeys, Int32 startFrom) at System.Data.Objects.ObjectContext.RefreshEntities(RefreshMode refreshMode, IEnumerable collection) at System.Data.Objects.ObjectContext.Refresh(RefreshMode refreshMode, Object entity) at System.Data.Entity.Internal.InternalEntityEntry.Reload() at System.Data.Entity.Infrastructure.DbEntityEntry.Reload() at Chilli.Model.ObjectContext.SafeReload(Object o) in c:\projects\MyProject\Chilli.Model\ObjectContext_Refresh.cs:line 19 InnerException:

greg
  • 1,289
  • 2
  • 14
  • 33
ajd
  • 423
  • 4
  • 11
  • Similar question http://stackoverflow.com/q/8044554/1341477 and I would look at the link in the answer but avoid diabling proxies as he did... – Dave Williams Jun 20 '13 at 09:16
  • Also could it be anything related to 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 – Dave Williams Jun 20 '13 at 09:21
  • Thanks Dave! I did wonder if the proxies were not calling the PropertyChanging and PropertyChanged events during a Reload()... which didn't stop me spending 2 days recoding the whole app to use INotifyProperyChanging as well as INotifyPropertyChanged i can test – ajd Jun 21 '13 at 07:58
  • i've posted some test code at http://softwaremechanik.wordpress.com/2013/06/21/ef-wpf-datagrid-reload-causes-invalidoperationexception-investigation/ but i can't reproduce the effect in a test. Anyhow, thanks for your help Dave, please add that as an answer and I'll accept it... – ajd Jun 21 '13 at 09:42
  • No problem hope it helped somehow. – Dave Williams Jun 21 '13 at 11:00

1 Answers1

0

It seems to be an issue experienced when using proxies. It may be possible to resolve by disabling proxies, or possibly by not using change tracking (which is apparently discouraged now).

As in my comments on the question there are others experiencing similar issues which can be found here:

Stack Overflow

Microsoft Connect

Community
  • 1
  • 1
Dave Williams
  • 2,166
  • 19
  • 25