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

ef-code-first entity-framework mvvm observablecollection


When using EF CodeFirst and connecting my user interface to anObservableCollection<T> The UI will change when entities are added to the DBContext but not when they are updated, according to the DBSet.Local property's here recommendation.

It seems that theDBSet.Local I have not informed collection that my entities have changed. Do I have to implement EF Code-First while utilizing it?INotifyPropertyChanged for every field I have? I haven't seen this in any of the Code-First examples, and it seems to defeat the point, but I might be wrong.

If you believe the code needs some examples, I can submit some.ObservableCollection There must be a problem with my Entity classes or something since I should be getting update alerts.

Edit So, just to be certain, if my ViewModel has the following code

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 further suppose that my View contains a Listbox with Clients as the SelectedItem and Clients as the Itemssource. Even when I use INotifyPropertyChanged in my ViewModel, do I still need to implement it in my Model?

8/9/2011 1:57:52 PM

Accepted Answer


Every element that you wish to be able to update the user interface when one of its properties changes must support INotifyPropertyChange.

Because there is no CollectionChanged event related to a property change of one of those items, the UI is unaware of it even if it is aware when the collection changes.

Also see: Modeling using Entity Framework CTP5 Code First and WPF MVVM

5/23/2017 12:23:34 PM

Popular Answer

This is a poor response, but I'm providing it nonetheless to help others understand the question.

You must implement INotifyPropertyChanged in your model for controls to get notifications of their modification, thus it would appear that the answer is "YES" (thus allow controls to update when they are changed).

But if the aforementioned is accurate, how come a TextBox that is databound to an object may affect other controls?

Try it! For instance, you may use UpdateSourceTrigger=PropertyChanged to connect the Text property of a TextBox to the SelectedItem of a DataGrid (which is linked to an ObservableCollection from a.Local). The text in the DataGrid row will update in real-time as soon as you run, select, and change a row in the TextBox. How does the text box make the DataGrid make that change?

The preceding example's binding code is provided here:

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

This also works with a TextBox on a different view that has a TextBox that is bound to the entity, as long as the entity is collected from the SelectedItem and handed in to the second view. (Manages to update the DataGrid in the first window in real time!)

This is very awesome! I now wish I could figure out how to do it in code SelectedItem. No matter what I do, the DataGrid will not be updated, therefore Name = "New Name." (I can't directly update the DataGrid since I use MVVM and don't have access to it.)

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

Related Questions


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