如何在.NET CORE中更改默认的ASP.NET标识表名称?

时间:2017-01-03 11:30:04

标签: asp.net-mvc asp.net-core identity

我已经开始使用.NET Core,在MVC 5中我更改了默认表名,例如:AspNETUsers以这种方式运行到用户并完美地工作: 在 IdentityModels 类中,我认为:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("Users").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<ApplicationUser>().ToTable("Users").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
    }

但它在.NET CORE(MVC 6)中不起作用。 谁能帮我? 非常感谢。

10 个答案:

答案 0 :(得分:17)

尝试将绑定更改为

builder.Entity<ApplicationUser>(entity =>
       {
           entity.ToTable(name:"Users");
           entity.Property(e => e.Id).HasColumnName("UserId");

       });

答案 1 :(得分:17)

- 要更改这些表的名称(IdentityUserRole <Tkey>,IdentityUserToken <Tkey>,IdentityRoleClaim <Tkey>,IdentityUserClaim <Tkey>,IdentityUserLogin <Tkey>)指定TKey的类型(用于主键的类型)默认情况下是字符串(GUID),即使您没有更改它。

- 如果要将主键从GUID更改为int https://medium.com/@goodealsnow/asp-net-core-identity-3-0-6018fc151b4

protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);

        builder.Entity<ApplicationUser>(entity =>
        {
            entity.ToTable(name: "User");         
        });

        builder.Entity<IdentityRole>(entity =>
        {
            entity.ToTable(name: "Role");
        });
        builder.Entity<IdentityUserRole<string>>(entity =>
        {
            entity.ToTable("UserRoles");
          //in case you chagned the TKey type
          //  entity.HasKey(key => new { key.UserId, key.RoleId });
        });

        builder.Entity<IdentityUserClaim<string>>(entity =>
        {
            entity.ToTable("UserClaims");
        });

        builder.Entity<IdentityUserLogin<string>>(entity =>
        {
            entity.ToTable("UserLogins");
             //in case you chagned the TKey type
          //  entity.HasKey(key => new { key.ProviderKey, key.LoginProvider });       
 });

        builder.Entity<IdentityRoleClaim<string>>(entity =>
        {
            entity.ToTable("RoleClaims");

        });

        builder.Entity<IdentityUserToken<string>>(entity =>
        {
            entity.ToTable("UserTokens");
            //in case you chagned the TKey type
           // entity.HasKey(key => new { key.UserId, key.LoginProvider, key.Name });

        });
    }

答案 2 :(得分:3)

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>(b =>
            {
                b.ToTable("ANYName");
            });
            modelBuilder.Entity<IdentityUserRole<string>>(b =>
            {
                b.ToTable("ANYName");
            });
            modelBuilder.Entity<IdentityRole<string>>(b =>
            {
                b.ToTable("ANYName");
            });
            modelBuilder.Entity<IdentityUserClaim<string>>(b =>
            {
                b.ToTable("ANYName");
            });
            modelBuilder.Entity<IdentityUserLogin<string>>(b =>
            {
                b.ToTable("ANYName");
            });
            modelBuilder.Entity<IdentityUserToken<string>>(b =>
            {
                b.ToTable("ANYName");
            });
            modelBuilder.Entity<IdentityRole>(b =>
            {
                b.ToTable("ANYName");
            });
            modelBuilder.Entity<IdentityRoleClaim<string>>(b =>
            {
                b.ToTable("ANYName");
            });
            modelBuilder.Entity<IdentityUserRole<string>>(b =>
            {
                b.ToTable("ANYName");
            });
        }

在ApplicationDbContext中添加此方法并进行add-migration创建更改,然后更新数据库以保存更改。

答案 3 :(得分:2)

ASP.Net Core 2 / 2.1的完整列表,基于@ ahmed-al-jabry的答案。

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<User>(entity => { entity.ToTable(name: "User"); });
    modelBuilder.Entity<IdentityRole>(entity => { entity.ToTable(name: "Role"); });
    modelBuilder.Entity<IdentityUserRole<string>>(entity => { entity.ToTable("UserRoles"); });
    modelBuilder.Entity<IdentityUserClaim<string>>(entity => { entity.ToTable("UserClaims"); });
    modelBuilder.Entity<IdentityUserLogin<string>>(entity => { entity.ToTable("UserLogins"); });
    modelBuilder.Entity<IdentityUserToken<string>>(entity => { entity.ToTable("UserToken"); });
    modelBuilder.Entity<IdentityRoleClaim<string>>(entity => { entity.ToTable("RoleClaim"); });
}

答案 4 :(得分:2)

仅出于文档目的,对于在未来几年里担任此职位的人(例如我XD),问题的答案:

如何在.NET CORE中更改默认的ASP.NET Identity表名称?

可以这样解决

   //Repeat with each table
   builder.Entity<ApplicationUser>(entity =>
   {
       entity.ToTable(name:"Users");
       entity.Property(e => e.Id).HasColumnName("UserId");

   });

或者可以这样解决

        modelBuilder.Entity<IdentityUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<ApplicationUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        modelBuilder.Entity<IdentityUserRole>().ToTable("MyUserRoles");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("MyUserLogins");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("MyUserClaims");
        modelBuilder.Entity<IdentityRole>().ToTable("MyRoles");

但是您可以简单地使用Alexandru Bucur在他的博客上给出的方法,并在netcore 2.2上进行测试

         foreach (var entityType in modelBuilder.Model.GetEntityTypes())
         {
            var table = entityType.Relational().TableName;
             if (table.StartsWith("AspNet"))
             {
                 entityType.Relational().TableName = table.Substring(6);
             }
         };

但这不是对netcore> 2.2的更长支持,因此,我需要对其进行修复,这是在NetCore> 2.2上的功能方式

        foreach (var entityType in modelBuilder.Model.GetEntityTypes())
        {
            var tableName = entityType.GetTableName();
            if (tableName.StartsWith("AspNet"))
            {
                entityType.SetTableName(tableName.Substring(6));
            }
        }

选择自己喜欢的事物并享受它,HappyCoding

答案 5 :(得分:1)

应用更改还需要两个步骤:

  • 应用添加迁移
  • 更新数据库

答案 6 :(得分:1)

要更新默认表名IdentityModels类MVC

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("Users").HasKey(x => x.Id);
        modelBuilder.Entity<ApplicationUser>().ToTable("Users")HasKey(x => x.Id);
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles").HasKey(x => x.RoleId);;
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins").HasKey(x => x.UserID);;
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims")HasKey(x => x.Id);;
        modelBuilder.Entity<IdentityRole>().ToTable("Roles")HasKey(x => x.Id);;
    }

答案 7 :(得分:0)

我要添加第二个答案,因为很多人在尝试更改.NET core 2.1中的表名时都会遇到此问题

该过程在Microsoft docs中有很好的解释。

对于那些需要快速修复的人:

  1. 继承要更改的所有模型(默认Microsoft授权随附7个模型)。例如,将AspNetUsers更改为User,将AspNetRoles更改为Role,您可以在现有模型中执行以下操作:

    soup=bs.BeautifulSoup(sauce,"xml")
    

在此示例中,我还更改了主键类型,因为默认值为public partial class User : IdentityUser<int> { // code } public partial class Role : IdentityRole<int> { // code }

  1. 在您的上下文中,继承自IdentityDbContext并使用与参数相同的类型:

    nvarchar
  2. 接下来,我们必须更新public class AppDbContext : IdentityDbContext<User, Role, int> { // code } 中的ConfigureServices以使用新的StartUp类:

    User

然后,如果需要,您可以迁移以更新/创建数据库。取决于这是新项目还是旧项目。

  

注意:如果您当前在项目中使用身份验证,例如 services.AddDefaultIdentity<User, Role>() .AddEntityFrameworkStores<AppDbContext>(); UserManager,则必须将其通用参数更改为新的参数,例如:

SignInManager

希望有帮助:)

答案 8 :(得分:0)

对于MVC5 IdentityModels

<typeof>

答案 9 :(得分:0)

有一个额外的说明: 我自定义了所有的身份表名称,但没有应用。基于Microsoft reference,您必须在自定义之前使用base.OnModelCreating(builder);

要更改表和列的名称,请调用base.OnModelCreating。然后,添加配置以覆盖任何默认设置。例如,更改所有身份表的名称:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<IdentityUser>(b =>
    {
       b.ToTable("MyUsers");
    });

    modelBuilder.Entity<IdentityUserClaim<string>>(b =>
    {
        b.ToTable("MyUserClaims");
    });

    modelBuilder.Entity<IdentityUserLogin<string>>(b =>
    {
        b.ToTable("MyUserLogins");
    });

    modelBuilder.Entity<IdentityUserToken<string>>(b =>
    {
        b.ToTable("MyUserTokens");
    });

    modelBuilder.Entity<IdentityRole>(b =>
    {
        b.ToTable("MyRoles");
    });

    modelBuilder.Entity<IdentityRoleClaim<string>>(b =>
    {
        b.ToTable("MyRoleClaims");
    });

    modelBuilder.Entity<IdentityUserRole<string>>(b =>
    {
        b.ToTable("MyUserRoles");
    });
}