INotifyPropertyChanged not called correctly with Entity Framework Core

c# entity-framework entity-framework-core inotifypropertychanged mvvm

Question

I have a very simple entity which is used (as part of a view model) to display UI in my MVVM UWP app. I am using lazy loading proxies over a SQLite database.

The entity has two properties - Name and ID. Both are correctly raising the PropertyChanged event.

I am trying to implement a rename method, which presents UI to rename the entity. This works correctly and the database is updated, however the UI binding does not update.

I bound to the PropertyChanged event to try and see what was happening and I've noticed some odd behaviour. When the name is changed, the sender is the proxy and the proxy has the previous name. If I change the name again I see the first rename as the Name property.

If I remove Entity Framework from the equation and populate the UI with the same entity types, the app works as I would expect. For some reason PropertyChanged is being raised by EF before the proxy value has been updated.

I have tried using ChangeTrackingStrategy with no effect (like this):

        modelBuilder.Entity<Category>()
            .HasChangeTrackingStrategy(ChangeTrackingStrategy.ChangedNotifications);

How can I fix this?

1
0
8/7/2019 10:09:00 PM

Accepted Answer

For anyone else stumbling across issues with bindings using Entity Framework with observable objects, my underlying issue was that the SetProperty method, which all properties call to update, was marked virtual in my base object.

The effect was that the EF proxy was overriding this, which caused weird things to happen - the UI not updating, typing in text boxes and characters at the end moving to the start, etc.

Note that EF uses DynamicProxy under the covers, which will proxy all virtual methods.

0
10/17/2019 8:54:40 AM


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