When using EF Code-First, do I need to implement INotifyPropertyChanged?

ef-code-first entity-framework mvvm observablecollection

Question

I'm noticing that when using EF CodeFirst, and binding my UI to an ObservableCollection<T> via the DBSet.Local property recommended here, that the UI will update when entities are added to the DBContext but not when they are modified.

It would seem that the DBSet.Local collection is not being notified that my entities have changed. When using EF Code-First do I need to implement INotifyPropertyChanged for all of my fields? I have not seen this in any of the examples on Code-First and it seems to go against the purpose, but perhaps I'm mistaken.

I can post some example code if you feel that the ObservableCollection should be receiving change notifications and that something must be wrong in my Entity classes or something.

Edit So, just to be sure, if I have code like the following in my ViewModel

private ClientContext clientContext;
private Client client;
public Client Client
{
    get { return client; }
    set { 
          client = value; 
          NotifyOfPropertyChange(() => Client);
          NotifyOfPropertyChange(() => Clients);
        }
}
public ObservableCollection<Client> Clients { get { return clientContext.Clients.Local; }}

Let's also assume my View has a Listbox with Itemssource set to Clients, and SelectedItem set to Client. I still need to implement INotifyPropertyChanged in my Model, even though I'm using it in my ViewModel?

1
1
8/9/2011 1:57:52 PM

Accepted Answer

Yes,

Every element that you want to update the UI when a property of it is modified must implement INotifyPropertyChange.

The reason for this is, the UI knows when the collection changes, but if the property of one of those elements changes, there is no CollectionChanged event associated with it, so the UI is ignorant

See also: Entity Framework CTP5 Code First, WPF - MVVM modeling

3
5/23/2017 12:23:34 PM

Popular Answer

This is a bad answer, but I'm including it here to contribute to understanding of the question.

It would seem that the answer would be "YES" you must implement INotifyPropertyChanged in your model for controls to get notified of their change (thus allow controls to update when they are changed).

But if the above is true why can a TextBox databound to a entity cause changes in other controls.

Try it! For example you can bind a TextBox's Text property to the SelectedItem of a DataGrid (which is bound to a ObservableCollection from a .Local) with UpdateSourceTrigger=PropertyChanged. Now run and select a row in the DataGrid and edit the TextBox and the text in the DataGrid row will update realtime! How does the text box force that change in the DataGrid!?!?!

Here is the binding code of the above example:

<TextBox Text="{Binding Path=SelectedItem.Name, UpdateSourceTrigger=PropertyChanged}" />

This works with a TextBox not just on the same View, but even if the entity is gathered from SelectedItem and passed in to a second view which then has a TextBox that binds to the entity. (Manages to update the first window's DataGrid realtime!)

This is super cool! Now I wish I could find a way to in code SelectedItem.Name = "New Name", because no matter what I try that will not update the DataGrid. (I can't refresh the DataGrid directly because I'm using MVVM and have no access to it)

Note: I'm using EF4.1 with DBContext & POCO entities (ie. simple classes that do not implement any interfaces)



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow