2
  1. This same question was asked and has not been answered after 12 days...

  2. I have looked at this which uses "ToTable" as an update to the question.

  3. I have looked at this which appears to be out of date.

I want to change the table names of the identity 3.0 tables - ASP.NET Core.

So far, using the "ToTable" option's (No. 2 above) update, I have managed to corrupt my lockfile and have as a result corrupted the project. The third option is out of date.

I created a vanilla project - no changes just created via VS2015 with identity 3.0

I then tried this:

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

I then checked the updated migration to see if the table names had changed and they havent.

I am using 1.0.0-rc1-update2 at the moment.

How do you change the names of these tables?

Community
  • 1
  • 1
si2030
  • 3,895
  • 8
  • 38
  • 87
  • 1
    Possible duplicate http://stackoverflow.com/questions/34523066/how-can-i-change-the-table-names-used-by-asp-net-identity-3-vnext – devfric Apr 26 '16 at 07:05
  • Yep.. missed that one. I have got this to work however I cannot get " builder.Entity>().ForSqlServerToTable("RoleClaims");" to work. All the others work but when I add this in it doesnt.. – si2030 Apr 27 '16 at 02:13

2 Answers2

10

Try this code, it changes all asp.net identify default table names to custom table names e.g. User, Role, UserClaim ... etc. it works for me.

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

}
Ahm3d Said
  • 802
  • 9
  • 20
dinner689
  • 381
  • 1
  • 4
  • 10
5

You need to include all of the generic type args for it to work. You will also then need to change User and Role to include the generic type args too

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>
Matt
  • 389
  • 1
  • 8
  • 15