开发者

EF Navigation properties don't raise OnPropertyChanging and OnPropertyChanged Events

EF Navigation properties don't raise OnPropertyChanging and OnPropertyChanged Events.

When looking into the entity framework auto-generated code a simple property will have a setter that looks like this:

    Set
        OnNameChanging(value)
        ReportPropertyChanging("Name")
        _Name = StructuralObject.SetValidValue(value, false)
        ReportPropertyChanged("Name")
        OnNameChanged()
    End Set
开发者_运维问答

However, navigation properties have a setter like this:

    Set
        If (Not value Is Nothing)
            CType(Me,EntityWithRelationships).RelationshipManager.InitializeRelatedCollection(Of DataType)("MyDatabase.FK_FKTable_PrimaryKeyTable", "FKTable", value)
        End If
    End Set

Seems like the navigation properties should call just call the 4 change notification methods. Do you have any insights on why the EF generater would be implemented in this manner? Is there an option somewhere to turn on notification or a work-around? Also, I assume that editing the designer generated code is bad practice, correct?

Thanks.


The OnPropertyChanging and OnPropertyChanged events are necessary to let ObjectStateManager know that some property of the object has changed.
In the case of Navigation Properties there is no need to do this, because RelationshipManager performs the necessary actions in the InitializeRelatedCollection method.
This is a part of the EntityCollection and EntityReference mechanism.
Any edits of the *.Designer.cs file will be discarded after any code generator run. You can try customizing T4 templates as described here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜