【发布时间】:2015-08-25 13:56:13
【问题描述】:
默认情况下,UserRoles 通过UserId and RoleId 映射了两个表User 和Role,并在上述列上具有复合键。
我通过添加另外两个实体关系来定制它:
public class CustomUserRole : IdentityUserRole<int>
{
[Key]
public int MyEntityId { get; set; }
public virtual MyEntity MyEntity { get; set; }
}
我有这个迁移脚本:
public override void Up()
{
CreateTable(
"dbo.MyEntity",
c => new
{
Id = c.Int(nullable: false, identity: true),
Name = c.String(),
})
.PrimaryKey(t => t.Id);
AddColumn("dbo.UserRoles", "MyEntityId", c => c.Int(nullable: false));
CreateIndex("dbo.UserRoles", "MyEntityId");
AddForeignKey("dbo.UserRoles", "MyEntityId", "dbo.MyEntity", "Id", cascadeDelete: true);
}
在我更新数据库后,UserRole 表中多了一个名为MyEntityId 的列,但它被添加为FK。我期待有三个复合主键UserId and RoleId and MyEntityId。
如何在不手动触摸数据库的情况下先使用实体代码编写PK:UserId and RoleId and MyEntityId?
【问题讨论】:
标签: entity-framework asp.net-mvc-5 code-first entity-framework-6.1