在 EF6 中创建复合键的正确数据注释是什么?
What is the proper data annotation to create a composite key in EF6?
我正在使用 ASP.NET MVC 5 和 Entity Framework v6。在这个项目中,我还使用了 ASP.NET 身份,在 SQL 服务器中创建了一个名为 dbo.AspNetUserRoles
的 table。 table 允许具有相同 UserId
的多个用户角色记录。在 SSMS 中显示以下列和键:
我正在尝试使用 EF 6 在 ASP.NET MVC 中创建一个类似的 table。这是当前的 class:
Public Class SqlDomainUserRole
<Key>
<Column(Order:=10)>
Public Property SqlUserId As String
<Key>
<Column(Order:=20)>
Public Property SqlRoleId As String
Friend Function ToDomainUserRole() As DomainUserRole
End Function
Friend Function ToSqlDomainUserRole(DomainUserRole As DomainUserRole) As SqlDomainUserRole
End Function
End Class
在 SSMS 中显示如下,缺少 dbo.AspNetUserRoles
具有的附加键:
我无法添加具有相同 SqlUserID
的多条记录。我认为这是因为我没有正确设置数据注释以在 SQL 服务器中创建正确的密钥结构;或者我可能缺少其他一些基本概念。
如果是数据注释问题,我似乎无法在 EF 文档中找到解释如何创建我需要的组合键的位置,该键结合了 SqlUserID
和 SqlRoleID
我的 class 以上。
什么是正确的数据注释,以便我的新 table 可以像 dbo.AspNetUserRoles
table 一样运行?
jmcilhinney
的第一条评论指出了正确的道路。为解决该问题,定义了以下 classes,尽管请参阅底部的注释,因为这些 class 定义必须等待先前编码的 class 被删除:
Public Class SqlDomainRole
<Key>
Public Property SqlDomainRoleID As String
<ForeignKey("SqlDomainUsers")>
Public Property SqlDomainUsers As List(Of SqlDomainUser)
End Class
Public Class SqlDomainUser
<Key>
Public Property SqlDomainUserId As String
<ForeignKey("SqlDomainRoles")>
Public Property SqlDomainRoles As New List(Of SqlDomainRole)
End Class
EF 然后生成了以下迁移代码,该代码创建了先前在代码中定义的 table,如初始问题中所述:
Public Overrides Sub Up()
CreateTable(
"dbo.SqlDomainUserSqlDomainRoles",
Function(c) New With
{
.SqlDomainUser_SqlUserId = c.String(nullable := False, maxLength := 128),
.SqlDomainRole_SqlRoleID = c.String(nullable := False, maxLength := 128)
}) _
.PrimaryKey(Function(t) New With { t.SqlDomainUser_SqlUserId, t.SqlDomainRole_SqlRoleID }) _
.ForeignKey("dbo.SqlDomainUsers", Function(t) t.SqlDomainUser_SqlUserId, cascadeDelete := True) _
.ForeignKey("dbo.SqlDomainRoles", Function(t) t.SqlDomainRole_SqlRoleID, cascadeDelete := True) _
.Index(Function(t) t.SqlDomainUser_SqlUserId) _
.Index(Function(t) t.SqlDomainRole_SqlRoleID)
End Sub
但是需要先删除之前在代码中定义的table SqlDomainUserRole
,因为上面迁移更新时存在重复的key导致失败。删除 SqlDomainUserRole
后,使用该删除处理迁移,然后处理上面显示的迁移并且 运行 正确。
我正在使用 ASP.NET MVC 5 和 Entity Framework v6。在这个项目中,我还使用了 ASP.NET 身份,在 SQL 服务器中创建了一个名为 dbo.AspNetUserRoles
的 table。 table 允许具有相同 UserId
的多个用户角色记录。在 SSMS 中显示以下列和键:
我正在尝试使用 EF 6 在 ASP.NET MVC 中创建一个类似的 table。这是当前的 class:
Public Class SqlDomainUserRole
<Key>
<Column(Order:=10)>
Public Property SqlUserId As String
<Key>
<Column(Order:=20)>
Public Property SqlRoleId As String
Friend Function ToDomainUserRole() As DomainUserRole
End Function
Friend Function ToSqlDomainUserRole(DomainUserRole As DomainUserRole) As SqlDomainUserRole
End Function
End Class
在 SSMS 中显示如下,缺少 dbo.AspNetUserRoles
具有的附加键:
我无法添加具有相同 SqlUserID
的多条记录。我认为这是因为我没有正确设置数据注释以在 SQL 服务器中创建正确的密钥结构;或者我可能缺少其他一些基本概念。
如果是数据注释问题,我似乎无法在 EF 文档中找到解释如何创建我需要的组合键的位置,该键结合了 SqlUserID
和 SqlRoleID
我的 class 以上。
什么是正确的数据注释,以便我的新 table 可以像 dbo.AspNetUserRoles
table 一样运行?
jmcilhinney
的第一条评论指出了正确的道路。为解决该问题,定义了以下 classes,尽管请参阅底部的注释,因为这些 class 定义必须等待先前编码的 class 被删除:
Public Class SqlDomainRole
<Key>
Public Property SqlDomainRoleID As String
<ForeignKey("SqlDomainUsers")>
Public Property SqlDomainUsers As List(Of SqlDomainUser)
End Class
Public Class SqlDomainUser
<Key>
Public Property SqlDomainUserId As String
<ForeignKey("SqlDomainRoles")>
Public Property SqlDomainRoles As New List(Of SqlDomainRole)
End Class
EF 然后生成了以下迁移代码,该代码创建了先前在代码中定义的 table,如初始问题中所述:
Public Overrides Sub Up()
CreateTable(
"dbo.SqlDomainUserSqlDomainRoles",
Function(c) New With
{
.SqlDomainUser_SqlUserId = c.String(nullable := False, maxLength := 128),
.SqlDomainRole_SqlRoleID = c.String(nullable := False, maxLength := 128)
}) _
.PrimaryKey(Function(t) New With { t.SqlDomainUser_SqlUserId, t.SqlDomainRole_SqlRoleID }) _
.ForeignKey("dbo.SqlDomainUsers", Function(t) t.SqlDomainUser_SqlUserId, cascadeDelete := True) _
.ForeignKey("dbo.SqlDomainRoles", Function(t) t.SqlDomainRole_SqlRoleID, cascadeDelete := True) _
.Index(Function(t) t.SqlDomainUser_SqlUserId) _
.Index(Function(t) t.SqlDomainRole_SqlRoleID)
End Sub
但是需要先删除之前在代码中定义的table SqlDomainUserRole
,因为上面迁移更新时存在重复的key导致失败。删除 SqlDomainUserRole
后,使用该删除处理迁移,然后处理上面显示的迁移并且 运行 正确。