我遇到ASP.NET身份问题,在我的Role和UserAcccount表之间创建了不正确的表关系
这是我的OnModelCreating:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim").ToTable("UserClaim").Property(p => p.Id).HasColumnName("UserClaimID");
modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
modelBuilder.Entity<IdentityRole>().ToTable("Role").Property(p => p.Id).HasColumnName("RoleID");
modelBuilder.Entity<ApplicationUser>().ToTable("UserAccount").ToTable("UserAccount").Property(p => p.Id).HasColumnName("UserID");
}
以下是生成的迁移:
CreateTable(
"dbo.Role",
c => new
{
RoleID = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
Description = c.String(),
Discriminator = c.String(nullable: false, maxLength: 128),
ApplicationUser_Id = c.String(maxLength: 128),
})
.PrimaryKey(t => t.RoleID)
.ForeignKey("dbo.UserAccount", t => t.ApplicationUser_Id)
.Index(t => t.Name, unique: true, name: "RoleNameIndex")
.Index(t => t.ApplicationUser_Id);
以下是我想看到的内容:
CreateTable(
"dbo.Role",
c => new
{
RoleID = c.String(nullable: false, maxLength: 128),
Name = c.String(nullable: false, maxLength: 256),
Description = c.String(),
Discriminator = c.String(nullable: false, maxLength: 128),
UserID = c.String(maxLength: 128),
})
.PrimaryKey(t => t.RoleID)
.ForeignKey("dbo.UserAccount", t => t.UserID)
.Index(t => t.Name, unique: true, name: "RoleNameIndex")
.Index(t => t.UserID);
我已经引用了其他几个StackOverflow帖子和其他资源几乎解决了我的问题,但并不完全: EF 6.1 Code First - the number of columns specified must match the number of primary key columns,MapKey vs HasForeignKey Difference - Fluent Api,https://msdn.microsoft.com/en-us/data/hh134698.aspx。