Entity Framework 4.1 Code First - Keys/Navigation Properties

柔情痞子 提交于 2020-01-06 08:17:13

问题


Is this how you would setup a basic PK/FK relationship?

Do you have to define both the key and the navigation property?

Public Class Foo
    'PK
    Public Property FooID As Integer

    'Navigation Prop
    Public Overridable Property Bars As ICollection(Of Bar)

End Class

Public Class Bar
    'PK
    Public Property BarID As Integer
    'FK
    Public Property FooID As Integer

    'Navigation Prop
    Public Overridable Property Foo As Foo

End Class

回答1:


This is basic configuration if you want default conventions to take care of the mapping. But you can use fluent interface and define anything from these examples as valid relation:

Navigation property only on the parent:

Public Class Foo
    'PK
    Public Property FooID As Integer

    'Navigation Prop
    Public Overridable Property Bars As ICollection(Of Bar)   
End Class

Public Class Bar
    'PK
    Public Property BarID As Integer
End Class

Navigation propety only on the parent and FK property on the child:

Public Class Foo
    'PK
    Public Property FooID As Integer

    'Navigation Prop
    Public Overridable Property Bars As ICollection(Of Bar)
End Class

Public Class Bar
    'PK
    Public Property BarID As Integer
    'FK
    Public
    Property FooID As Integer
End Class

Navigation property on the child:

Public Class Foo
    'PK
    Public Property FooID As Integer
End Class

Public Class Bar
    'PK
    Public Property BarID As Integer

    'Navigation Prop
    Public Overridable Property Foo As Foo
End Class

Navigation property and FK property on the child:

Public Class Foo
    'PK
    Public Property FooID As Integer
End Class

Public Class Bar
    'PK
    Public Property BarID As Integer
    'FK
    Public Property FooID As Integer

    'Navigation Prop
    Public Overridable Property Foo As Foo
End Class

In addition you can make also map the realtion as optional.



来源:https://stackoverflow.com/questions/5534891/entity-framework-4-1-code-first-keys-navigation-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!