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?
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.