Entity Framework 4.1 Code First - Keys/Navigation Properties
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.
精彩评论