0

Entity Framework 6.x with a DbContext database first based model (built with Devart's Entity Developer (although I think that is irrelevant here)).

The datamodel and all business code related to it is in its own separate project and all of the entities are set to implement INotifyPropertyChanged and Changing. The PropertyChanged and PropertyChanging events are handled in separate partial classes for each entity.

When I edit an existing row od data the PropertyChanged event gets fired and I can for example set the value of some other field based on the changes to the one that has just been changed. This all works exactly as I would expect. However as soon as I move to what in effect will be a new record the PropertyChanged event is not getting fired so the logic that I have constructed to make edits easier isn't being implemented for new records.

I have put breakpoints on the PropertyChanged event and I see it fired on edits but not on new rows.

I presume that somewhere I have missed something obvious but I can't think what. Could anyone enlighten me as to what I need to do to get this to apply to new records as well as editing existing ones? I generally code in vb but do understand C# (at least the basics!)

Thanks

Dom Sinclair
  • 2,458
  • 1
  • 30
  • 47
  • Are you registering the property changed event on initialization? Check if the event handler is null if that's the case it's probably because the object isn't bound to the UI since property changed only works with UI in place. – Rand Random Jul 01 '14 at 08:06
  • Pinned this down to the entity itself. The particular entity (typically the first one I tried this with which caused me to make a general assumption) has a composite primary key that is not set automatically. I'm in the process of ensuring that this gets set first, then I hope that the PropertyChanged event will work as it should. – Dom Sinclair Jul 01 '14 at 08:55

1 Answers1

2

Well, I can't find any modern information on this topic, but as far as I know Entity framework entity collections do not implement INotifyCollectionChanged or any other collection notifying mechanism.

INotifyPropertyChanged is designed to notify about the changes of the property value. When you have a collection property like

public ICollection<MyEntity> SomeEntities
{
    get {...} 
    set {... PropertyChanged(this, ...);}
}

and someone uses it to add the entity into it

entityContainer.SomeEntities.Add(new MyEntity());

then the PropertyChanged won't be raised, because the actual property is still the same collection - the setter method isn't used.

the ICollection<MyEntity> itself has to implement the INotifyCOllectionChanged interface (like ObservableCollection<T>)

If you want to have notification mechanics between different parts of your operation you should wrap your queries into some INotifyCollectionChanged implementers, that delegate deletion and addition to the DataContext - EntityFramework EntityCollection Observing CollectionChanged.

Community
  • 1
  • 1
Eugene Podskal
  • 10,270
  • 5
  • 31
  • 53