【问题标题】:EF Code First Migration unwanted column IdentityRole_IdEF Code First Migration 不需要的列 IdentityRole_Id
【发布时间】:2014-09-07 01:23:56
【问题描述】:

我使用的是 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);

【问题讨论】:

    标签: entity-framework ef-code-first


    【解决方案1】:

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

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

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

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

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

    那么你必须调用基地

    base.OnModelCreating(modelBuilder);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-29
      • 2021-09-23
      • 2015-07-08
      • 1970-01-01
      • 2011-09-27
      • 2011-10-22
      • 1970-01-01
      相关资源
      最近更新 更多