实体框架4.1代码优先 - 密钥/导航属性

时间:2011-04-04 05:29:00

标签: entity-framework entity-framework-4.1

这是你如何建立基本的PK / FK关系吗?

您是否必须同时定义键和导航属性?

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 个答案:

答案 0 :(得分:2)

如果您希望默认约定来处理映射,那么这是基本配置。但是您可以使用流畅的界面并将这些示例中的任何内容定义为有效关系:

仅在父级上的导航属性:

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

仅在子项的父级和FK属性上导航:

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

孩子的导航属性:

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

孩子的导航属性和FK属性:

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

此外,您还可以将实现映射为可选项。

相关问题