更改ASP.NET Core MVC6中的Identity 3.0表名称无法正常工作

si2*_*030 1 asp.net-core-mvc asp.net-identity-3 asp.net-core

  1. 这个同样的问题,有人问,12天后还没有得到答复...

  2. 我看过这个使用"ToTable"作为问题的更新.

  3. 我看过这个似乎已经过时的了.

我想更改标识3.0表的表名 - ASP.NET Core.

到目前为止,使用"ToTable"选项(上面的第2号)更新,我设法破坏了我的锁定文件,结果导致项目损坏.第三种选择已过期.

我创建了一个vanilla项目 - 没有通过带有身份3.0的VS2015创建的更改

然后我尝试了这个:

    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<IdentityUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        builder.Entity<ApplicationUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId");
        builder.Entity<IdentityRole>().ToTable("MyRoles");
    }
Run Code Online (Sandbox Code Playgroud)

然后,我检查了更新的迁移,以查看表名是否已更改,但他们没有.

我目前正在使用1.0.0-rc1-update2.

你如何更改这些表的名称?

din*_*689 9

尝试这个代码,它将所有asp.net标识的默认表名更改为自定义表名,例如User,Role,UserClaim ...等.它适用于我.

using Microsoft.AspNetCore.Identity;
...
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

     ...

     protected override void OnModelCreating(ModelBuilder modelBuilder)
     {
        base.OnModelCreating(modelBuilder);
        // Add your customizations after calling base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<ApplicationUser>().ToTable("User");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserLogin<string>>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaim");
        modelBuilder.Entity<IdentityUserToken<string>>().ToTable("UserToken");
      }

}
Run Code Online (Sandbox Code Playgroud)


Mat*_*att 5

您需要包含所有泛型类型参数才能使其正常工作。然后,您还需要更改用户和角色以也包含通用类型参数

public class BlahDbContext : IdentityDbContext<User, Role, long, UserClaim, UserRole, UserLogin, RoleClaim, UserToken>
{
    public BlahDbContext(DbContextOptions<BlahDbContext> options)
        : base(options)
    {
    }

    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<User>().ToTable("Users");
        builder.Entity<Role>().ToTable("Roles");
        builder.Entity<UserRole>().ToTable("UserRoles");
        builder.Entity<UserLogin>().ToTable("UserLogins");
        builder.Entity<UserClaim>().ToTable("UserClaims");

        builder.Entity<RoleClaim>().ToTable("RoleClaims");
        builder.Entity<UserToken>().ToTable("UserTokens");            
    }
}

public class User : IdentityUser<long, UserClaim, UserRole, UserLogin>
public class Role : IdentityRole<long, UserRole, RoleClaim>
Run Code Online (Sandbox Code Playgroud)