我目前正在开发一个MVC 5项目,我在其中创建了一个新表(代码优先)AspNetApplications(column1 ApplicationId,column2 ApplicationName)。
我想扩展AspNetUserRoles表,我希望将ApplicationId添加为外键列。
可以请任何人告诉我如何在代码优先方法中实现这一目标吗?
答案 0 :(得分:1)
在我的问题之上,我已成功将ApplicationId添加为AspNetUserRoles表中的外键列,如下所示
public class AspNetUserRoles : IdentityUserRole
{
[Key]
public string ApplicationId { get; set; }
[ForeignKey("ApplicationId")]
public AspNetApplications AspNetApplications { get; set; }
}
现在我可以在表中看到ApplicationId列,但是在AspNetUserRoles表中添加了Discriminator列的地方发生了奇怪的事情。可以请有人告诉我这里做错了什么。以下是我的迁移脚本
public partial class AspNetUserRolesApplicationId : DbMigration
{
public override void Up()
{
AddColumn("dbo.AspNetUserRoles", "ApplicationId", c => c.String(maxLength: 128));
AddColumn("dbo.AspNetUserRoles", "Discriminator", c => c.String(nullable: false, maxLength: 128));
CreateIndex("dbo.AspNetUserRoles", "ApplicationId");
AddForeignKey("dbo.AspNetUserRoles", "ApplicationId", "dbo.AspNetApplications", "ApplicationId");
}
public override void Down()
{
DropForeignKey("dbo.AspNetUserRoles", "ApplicationId", "dbo.AspNetApplications");
DropIndex("dbo.AspNetUserRoles", new[] { "ApplicationId" });
DropColumn("dbo.AspNetUserRoles", "Discriminator");
DropColumn("dbo.AspNetUserRoles", "ApplicationId");
}
}