2011-04-04 104 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 

另外,您還可以將實現映射爲可選映射。