Override Default Identity Table Names

asp.net-core asp.net-core-mvc asp.net-mvc entity-framework-core

Question

I would like to rename the default identity table names:

  • AspNetRoles
  • AspNetUserClaims
  • AspNetUserLogins
  • AspNetUserRoles
  • AspNetUsers

I understand how to do this using EF6:

 protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUser>()
            .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
        modelBuilder.Entity<User>()
            .ToTable("Users", "dbo").Property(p => p.Id).HasColumnName("User_Id");
    }

However I'm struggling with EF7 as the DbModelBuilder has been replaced with ModelBuilder. Any suggestions?

Accepted Answer

You must use ForSqlServer or ForRelational in the SqlServerPropertyBuilder to change the column name and in the modelBuilder to change the table name

modelBuilder.Entity<IdentityUser>()
                .Property(o => o.Id)
                .ForSqlServer()
                .Column("User_Id")

 modelBuilder.Entity<IdentityUser>()
                    .ForSqlServer()
                    .Table("User")

Update : For the beta 5 is not longer mandatory to use ForSqlServer


Popular Answer

Try

    builder.Entity<ApplicationUser>().ToTable("User");
    builder.Entity<ApplicationRole>().ToTable("Role");

    builder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
    builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim");
    builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogin");

    builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaim");
    builder.Entity<IdentityUserToken<string>>().ToTable("UserToken");

@noelbr



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why