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

时间:2016-04-26 03:24:08

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

  1. 这个same question被问及12天后没有得到答复......

  2. 我查看this使用“ToTable”作为问题的更新。

  3. 我看过this似乎过时了。

  4. 我想更改标识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");
        }
    

    然后,我检查了更新的迁移,以查看表名是否已更改,并且它们没有。

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

    如何更改这些表的名称?

2 个答案:

答案 0 :(得分:9)

尝试此代码,它会将所有asp.net标识的默认表名更改为自定义表名,例如用户,角色,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");
      }

}

答案 1 :(得分:4)

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

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>