Ich habe eine ASP MVC 5-Anwendung, aber der Model Builder erstellt doppelte Fremdschlüssel für die IdentityUserRole-, IdentityUserClaim- und IdentityUserLogin-Tabellen.ASP.NET-Identity OnModelCreating Modelbuilder Doppelte Fremdschlüssel generieren
zB in der generierten Migrationstabelle hat unter RoleId sowie 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);
In meinem fließend api Ich habe dies wie folgt definiert:
public IdentityUserRoleConfiguration()
{
HasKey(x => new { x.RoleId, x.UserId });
ToTable("UserRole");
}
Meine Modellbauer Klasse:
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());
}
}
Ich habe die base.OnModelCreating(modelBuilder);
Aussage auskommentiert, weil sie werfen s die folgende Fehlermeldung, wenn ich eine Migration ausgeführt:
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.