EF 6,代码优先技术。
两个类定义为:
Public Class SqlLayerTypeSet
Public Overridable Property SqlLayerTypeSetID As Integer
Public Overridable Property Name As String
Public Overridable Property Desc As String
Public Property ConferenceTypes As New List(Of SqlConferenceType)
End Class
Public Class SqlConferenceType
Public Property ID As Integer
Public Property Name As String
Public Property Desc As String
End Class
播种后,表格分别如下:
我应该向Class SqlConferenceType
添加什么内容,以便我可以访问现有的外键SqlLayerTypeSet_SqlLayerTypeSetID
,该外键指向SqlLayerTypeSet
所属的SqlConferenceType
。
当我添加它应该是什么时,是否需要新的迁移?
我原以为EF允许您使用一种约定来定义类中的属性,该约定只是让您访问外键但实际上并没有更改表定义。我尝试向类添加属性的方式实际上是在表中添加另一列时,我真正想要的是访问EF代码首先创建的列。
我知道这是一个基本问题,但我似乎无法找到正确的SO帖子向我解释。
在首发后添加:
这是我添加的添加了对父级的访问权限的行:
Public Overridable Property SqlLayerTypeSet As SqlLayerTypeSet
但是,如果我尝试为父键添加特定属性,例如:
Public Property SqlLayerTypeSetID as Integer
这会创建额外的列。是否可以添加到Class SqlConferencetype
的唯一属性是导航属性,我只需要使用导航检索外键值,而不是能够定义一个只保存外键值的属性?
答案 0 :(得分:1)
Public Class SqlLayerTypeSet
Public Overridable Property SqlLayerTypeSetID As Integer
Public Overridable Property Name As String
Public Overridable Property Desc As String
<ForeignKey("SqlConferenceType")>
Public Overridable Property SqlConferenceTypeID as Integer 'Foreign key property
Public Overridable Property ConferenceTypes As New List(Of SqlConferenceType) 'Relationship property
End Class
Public Class SqlConferenceType
Public Property ID As Integer
Public Property Name As String
Public Property Desc As String
Public Overridable Property SqlLayerTypeSet as SqlLayerTypeSet 'Relationship property
End Class