【发布时间】:2016-07-13 11:17:38
【问题描述】:
我有一个 ASP MVC 5 应用程序,但模型构建器正在为 IdentityUserRole、IdentityUserClaim 和 IdentityUserLogin 表创建重复的外键。
例如,在下面生成的迁移表中,有 RoleId 和 IdentityRole_ID
CreateTable(
"dbo.UserRole",
c => new
{
RoleId = c.String(nullable: false, maxLength: 128),
UserId = c.String(nullable: false, maxLength: 128),
IdentityRole_Id = c.String(maxLength: 128),
ApplicationUser_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => new { t.RoleId, t.UserId })
.ForeignKey("dbo.Role", t => t.IdentityRole_Id)
.ForeignKey("dbo.User", t => t.ApplicationUser_Id)
.Index(t => t.IdentityRole_Id)
.Index(t => t.ApplicationUser_Id);
在我的 fluent api 中,我将其定义如下:
public IdentityUserRoleConfiguration()
{
HasKey(x => new { x.RoleId, x.UserId });
ToTable("UserRole");
}
我的模型构建器类:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
// Add some DBSets
public ApplicationDbContext()
: base("ApplicationDbContext", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
// base.OnModelCreating(modelBuilder);
modelBuilder.Configurations.Add(new IdentityUserRoleConfiguration());
modelBuilder.Configurations.Add(new IdentityUserLoginConfiguration());
modelBuilder.Configurations.Add(new IdentityRoleConfiguration());
modelBuilder.Configurations.Add(new IdentityUserClaimConfiguration());
modelBuilder.Configurations.Add(new ApplicationUserConfiguration());
}
}
我已注释掉 base.OnModelCreating(modelBuilder); 语句,因为它在我运行迁移时会引发以下错误:
A configuration for type 'Microsoft.AspNet.Identity.EntityFramework.IdentityUserRole' has already been added. To reference the existing configuration use the Entity<T>() or ComplexType<T>() methods.
【问题讨论】:
标签: asp.net-mvc entity-framework asp.net-identity ef-fluent-api