Phathutshedzo Khabubu
Phathutshedzo Khabubu

Reputation: 252

I want to rename the table names generated my ASPNET entity framework in .NetCore?

I keep getting the error : Cannot find the object "UserRoles" because it does not exist... I want to rename the table "AspNetUserRoles" to "UserRoles". Here is my code in the OnCreating method in the Application DbContext class

builder.Entity>().ToTable("UserRoles");

Upvotes: 1

Views: 1637

Answers (1)

Gerardo Grignoli
Gerardo Grignoli

Reputation: 15197

It's a little tricky for Identity because of how it use of generics.

  1. Add the ToTable mapping for the identity classes.
    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. Create a migration
dotnet ef migrations add RenameIdentityTables
  1. Update your database
dotnet ef database update

Upvotes: 5

Related Questions