0

I am having issues with ASP.NET Identity creating an incorrect table relationship between my Role and UserAcccount table

Here is my 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");
    }

Here is the resulting migration:

        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);

Here is what I want to see:

        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);

I have referenced several other StackOverflow posts and other resources that almost solve my problem, but not quite: 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.

Community
  • 1
  • 1
Mitch Stewart
  • 1,253
  • 10
  • 12

0 Answers0