I am using Entity Framework for my Model layer, and I’d like to hook up a PropertyChanged event that changes another property based on the OldValue and NewValue of the property. Is there a built-in method I could hook into for this?
The basic logic I want in my PropertyChanged event is
if (oldValue.DefaultPropertyA == this.PropertyA)
this.PropertyA = newValue.DefaultPropertyA;
You have the OnPropertyChanging partial method. There you will have access to the newvalue.
The old value, is the value of the property on that moment, so you can access it just trough the regular property.
The structure in your code generated file is like this:
As you can see, you can use the OnPropertyChanging and OnPropertyChanged partial methods to be notified of property changes.
If you want to change the value of the property that’s being set, you can’t do this in a partial method. If you really want this you should change your T4 template to inject this code in your property or use Code First so you have direct access