Ability to intercept and cancel changes to properties
(Note that the original description of this idea talked about making PropertyChangingEventArgs cancellable. Although this is an interesting request, it is not a change we could implement in EF. In fact, there is probably not way it can be implemented in .NET without defining a new standard interface for change tracking)
Rather than declining the idea, I am renaming it to capture the essence that is still potentially applicable to EF: provide a hook that can be used by an application or library to intercept what happens whenever EF detects that a property of a tracked object is being changed and allow for the change to be reverted.
When overriding the PropertyChangin in an entity object, there is no advantage as you can't stop the process from the handler (the partial OnPropertyNameChanging method can be used to throw an exception), since you have no way to guess the attempting set value which makes the PropertyChanging event almost worthless.
In addition, exceptions cost much more performance + is very rough compared to flagging e.Cancel.
So my suggestion is that PropertyChangingEventArgs should:
1) Provide the attempting set value (i.e. the value that the property will be set with if e.Cancel remains false)
2) Inherit from CancelEventArgs (sibling in System.ComponentModel namespace)
3) Provide the object who fired this event, so it can be handled in an external layer.
In this way, it will be much easier to globally validate entities without the need to implement individual OnSomePropertyChanging partial methods.
Please vote on the connection I posted about as well:
Reverting status from “under review” to “no status” since we aren’t currently working on this. We will still consider it for future releases.
@Tamus, I believe you're right. It shouldn't inherit from CancelEventArgs.
This will lead to inconsistency having the user cancel the property-set without notifying the caller.
BUT, but, but what really really important is, that the candidate value should be provided at the OnPropertyChanging event, then will throw an exception instead of cance, which makes more sense.
*SO FOR CONCLUSION*:
My original post should be edited and be:
"Provide the candidate value at the OnPropertyChanging method and in the PropertyChangingEventArgs"
- so user knows the what the candidate value is, otherwise, the PropertyChanging event makes no sense (getting the candidate value is a tough job and is very performance-expensive).
Please refrain from voting this up any more. First, use reflection to get the object located when OnPropertyChanging fires. Then again, use reflection to get the object located when OnPropertyChanged fires. Notice the endings. You can then compare the two to see the change.
It should not inherit from CancelEventArgs, as not only would this break .NET 1.1 to .NET 4.0 (or whatever version this is implemented) comparability, but the property that gets canceled needs to notify whatever is setting it that it didn't get set. And the only way to do that is to throw an exception (performance, yatta yatta yatta...bad!).
Also take a look at my suggestion here: http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1058879-associationchanging-event