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 RRS feed

  • Question

  • All,

    I have the following base class that all my entities inherit from:

        public abstract class cEntityBase:INotifyPropertyChanged
            public EntityState MyState
                    if (MyContext.Set(this.GetType()).Local.Cast<object>().Any(a => a == this))
                        return MyContext.ChangeTracker.Entries().First(a => a.Entity == this).State;
                    else return EntityState.Deleted;
            public event PropertyChangedEventHandler PropertyChanged;
            protected void RaisePropertyChanged(params string[] strProperties)
                strProperties.ForEach(s => OnPropertyChanged(new PropertyChangedEventArgs(s)));
            protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
                if (this != null)
                    Action act = () => PropertyChanged(this, e);
                        if (PropertyChanged != null)
                            if (System.Windows.Threading.Dispatcher.CurrentDispatcher.Thread != System.Threading.Thread.CurrentThread)
                    catch (InvalidOperationException) { }

    This way, I can see what each entity's EntityState is and bind this property to a DataTrigger.

    All of my Entity classes invoke RaisePropertyChanged("MyState") on each properties' setter.

    All of this works fine, as I can add a record and see visual ques in my UI that the record is new.  I can modify a record and also see visual ques in my UI that the record has been modified.

    The problem I seem to be having is, when I delete a record that cascade deletes to another record, RaisePropertyChanged("MyState") is being invoked, and this is causing WPF to requery the child entity's MyState property even when MyState has been deleted, which consequently throws an InvalidOperationException with the message noted above.  So, my question is, is there either 1) a better way to report PropertyChanged of this type to any object (including WPF objects) that are subscribed to the PropertyChanged event, or 2) some sort of condition I can test prior to attempting to return anything from the static property MyContext?

    Note that I've already tried surrounding my MyState code in Try...Catch, but that didn't seem to catch this exception (yet when I comment out all the code in MyState, the exception disappears).

    Thanks in advance.

    Friday, October 12, 2012 8:08 PM

All replies

  • Okay.  I figured it out (sort of).  Apparently, if I process my OnPropertyChanged asynchroniously (i.e. with Dispatcher.BeginInvoke(Action) I don't get this error and everything runs pretty smooth.  I guess now my question is why, assuming my Dispatcher thread and my Linq query (which I've actually simplified to just MyContext.Entry(this)) are on the same thread.  There must be something in DbContext that's going on that I don't see (perhaps cascading deletes being done in parallel), but that's just a guess at this point.


    Friday, October 12, 2012 9:17 PM
  • Hi,

    Could you please provide us with more information and maybe more sample codes?   Like what EF version are you using?

    Good day!


    Michael Sun [MSFT]
    MSDN Community Support | Feedback to us

    Monday, October 15, 2012 9:51 AM