实体框架4.1代码第一 - 键/导航属性 [英] Entity Framework 4.1 Code First - Keys/Navigation Properties
问题描述
您是否必须同时定义键和导航属性?
公共类Foo
'PK
公共财产FooID As Integer
'Navigation Prop
公共覆盖的财产酒吧作为ICollection(酒吧)
结束类
公共类酒吧
'PK
公共财产BarID As Integer
'FK
公共财产FooID As Integer
'导航支持
公共覆盖属性Foo为Foo
结束类
如果希望默认约定来处理映射,这是基本配置。但是,您可以使用流畅的界面,并将这些示例中的任何内容定义为有效关系:
仅在父级上导航属性:
公共类Foo
'PK
公共属性FooID As Integer
'导航支持
公共可覆盖的属性栏ICollection(Of Bar)
结束类
公共类Bar
'PK
公共属性BarID As Integer
结束类
仅在孩子的父母和FK属性上进行导航:
公共类Foo
'PK
公共属性FooID As Integer
'Navigation Prop
公共可覆盖的属性栏作为ICollection(Of酒吧)
结束类
公共类酒吧
'PK
公共财产BarID As Integer
'FK
公开
属性FooID As Integer
结束类
导航支柱对孩子有害:
公共类Foo
'PK
公共属性FooID As Integer
结束类
公共类Bar
'PK
公共属性BarID As Integer
'Navigation Prop
公共可覆盖属性Foo As Foo
结束类
孩子的导航属性和FK属性:
公共类Foo
'PK
公共财产FooID As Integer
结束类
公共课Bar
'PK
公共财产BarID As Integer
'FK
公共财产FooID As Integer
'导航支柱
公共覆盖属性Foo为Foo
结束类
此外,您还可以将实例映射为可选。
Is this how you would setup a basic PK/FK relationship?
Do you have to define both the key and the navigation property?
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.
这篇关于实体框架4.1代码第一 - 键/导航属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!