ASP.NET标识创建ApplicationUser_Id外键

时间:2016-08-25 15:47:00

标签: c# asp.net entity-framework asp.net-identity

我遇到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 columnsMapKey vs HasForeignKey Difference - Fluent Apihttps://msdn.microsoft.com/en-us/data/hh134698.aspx

0 个答案:

没有答案