开发者

Entity Framework 4.1 Code First - Keys/Navigation Properties

开发者 https://www.devze.com 2023-02-22 00:49 出处:网络
Is this how you would setup a basic PK/FK relationship? Do you have to define both the key and the navigation prop开发者_开发技巧erty?

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

Do you have to define both the key and the navigation prop开发者_开发技巧erty?

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


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.

0

精彩评论

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

关注公众号