2011-04-04 94 views
0

这是你如何设置一个基本的PK/FK关系?实体框架4.1代码第一键/导航属性

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

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 

回答

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 

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

相关问题