EF Code First Migration不需要的列IdentityRole_Id

时间:2014-07-28 13:07:32

标签: entity-framework ef-code-first

我正在使用EF 6.1.1 当我在我的数据库上使用代码优先迁移时,它会向AspNetUserRoles表添加两个不需要的列:IdentityRole_Id,IdentityUser_Id 我见过这个RemoveFromRole cannot work as expected,但没有帮助我。

我该如何摆脱它们?

这是我的OnModelCreating

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        if (modelBuilder == null)
        {
            throw new ArgumentNullException("modelBuilder");
        }

        // Keep this:
        modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");

        // Change TUser to ApplicationUser everywhere else - 
        // IdentityUser and ApplicationUser essentially 'share' the AspNetUsers Table in the database:
        EntityTypeConfiguration<ApplicationUser> table =
            modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");

        table.Property((ApplicationUser u) => u.UserName).IsRequired();

        // EF won't let us swap out IdentityUserRole for ApplicationUserRole here:            
        modelBuilder.Entity<ApplicationUser>().HasMany<IdentityUserRole>((ApplicationUser u) => u.Roles);
        modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) =>
            new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");

        // Leave this alone:
        EntityTypeConfiguration<IdentityUserLogin> entityTypeConfiguration =
            modelBuilder.Entity<IdentityUserLogin>().HasKey((IdentityUserLogin l) =>
                new
                {
                    UserId = l.UserId,
                    LoginProvider = l.LoginProvider,
                    ProviderKey
                        = l.ProviderKey
                }).ToTable("AspNetUserLogins");

        //entityTypeConfiguration.HasRequired<IdentityUser>((IdentityUserLogin u) => u.User);
        EntityTypeConfiguration<IdentityUserClaim> table1 = modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");

        //table1.HasRequired<IdentityUser>((IdentityUserClaim u) => u.User);

        // Add this, so that IdentityRole can share a table with ApplicationRole:
        modelBuilder.Entity<IdentityRole>().ToTable("AspNetRoles");

        // Change these from IdentityRole to ApplicationRole:
        EntityTypeConfiguration<ApplicationRole> entityTypeConfiguration1 =
            modelBuilder.Entity<ApplicationRole>().ToTable("AspNetRoles");

        entityTypeConfiguration1.Property((ApplicationRole r) => r.Name).IsRequired();
    }

它产生了这个:

CreateTable(
            "dbo.AspNetUserRoles",
            c => new
                {
                    UserId = c.String(nullable: false, maxLength: 128),
                    RoleId = c.String(nullable: false, maxLength: 128),
                    IdentityRole_Id = c.String(maxLength: 128),
                    IdentityUser_Id = c.String(maxLength: 128),
                })
            .PrimaryKey(t => new { t.UserId, t.RoleId })
            .ForeignKey("dbo.AspNetRoles", t => t.IdentityRole_Id)
            .ForeignKey("dbo.AspNetUsers", t => t.IdentityUser_Id)
            .Index(t => t.IdentityRole_Id)
            .Index(t => t.IdentityUser_Id);

1 个答案:

答案 0 :(得分:2)

假设您正在使用Identity EntityFramework(您的DbcontextClass:IdentityDbContext),那么我认为这一行是原因

modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) =>
        new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");

OnModelCreating中的所有代码都将创建与默认相同的身份表(只要您的应用程序上下文类派生自IdentityDbContext<ApplicationUser>

如果您出于某种原因想要使用覆盖OnModelCreating,例如

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
 {
       modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
 }

然后你必须打电话给基地

base.OnModelCreating(modelBuilder);