我想重命名在.NetCore中生成我的ASPNET实体框架的表名?

Pha*_*ubu 1 c# asp.net-core-mvc

我一直收到错误:无法找到对象"UserRoles",因为它不存在...我想将表"AspNetUserRoles"重命名为"UserRoles".这是我在Application DbContext类的OnCreating方法中的代码

builder.Entity>()ToTable( "的UserRole");

Ger*_*oli 5

这对身份来说有点棘手,因为它使用了泛型.

  1. 为标识类添加ToTable映射.
    public class ApplicationDbContext : IdentityDbContext
    {
       protected override void OnModelCreating(ModelBuilder builder)
       {
          base.OnModelCreating(builder);
          builder.Entity<ApplicationUser>().ToTable("Users");
          builder.Entity<IdentityRole>().ToTable("Roles");
          builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
          builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
          builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
          builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
          builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");
       }
    }
  1. 创建迁移
dotnet ef migrations add RenameIdentityTables
  1. 更新您的数据库
dotnet ef database update