2
  1. 提出了同样的问题,但 12 天后仍未得到回答......

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

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

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

到目前为止,使用“ToTable”选项(上面的第 2 项)更新,我已经设法破坏了我的锁定文件并因此破坏了项目。第三个选项已过时。

我创建了一个 vanilla 项目 - 刚刚通过 VS2015 使用身份 3.0 创建的没有任何更改

然后我尝试了这个:

    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 。

如何更改这些表的名称?

4

2 回答 2

10

试试这个代码,它将所有 asp.net 识别默认表名更改为自定义表名,例如用户、角色、用户声明......等等。它适用于我。

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");
      }

}
于 2017-01-13T12:05:52.917 回答
4

您需要包含所有泛型类型参数才能使其工作。然后,您还需要更改 User 和 Role 以包含通用类型 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>
于 2017-01-23T15:10:46.297 回答