The method used in asp.net identity 2 to alter the identity table names does not work in asp.net identity 3.
Asked
Active
Viewed 2,113 times
2 Answers
4
You can do this easily by changing the entity mapping with extension method ToTable("TableName")
on OnModelCreating
of your DbContext
:
And you don't need to use .ForSqlServerToTable()
, just .ToTable()
should work in any database.
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<User>().ToTable("Users"); // Your custom IdentityUser class
builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");
builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
builder.Entity<IdentityRole>().ToTable("Roles");
}
The only catch here is to remember to use the generics with the type of your identifier (string is default on AspNetCore.

Tanato
- 895
- 12
- 23
3
Modify the builder entities in OnModelCreating of your ApplicationDbContext, using ForSqlServerToTable extension method to change the desired table(s) name.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
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<ApplicationUser>().ForSqlServerToTable("Users");
builder.Entity<IdentityUserRole<string>>().ForSqlServerToTable("UserRoles");
builder.Entity<IdentityUserLogin<string>>().ForSqlServerToTable("UserLogins");
builder.Entity<IdentityUserClaim<string>>().ForSqlServerToTable("UserClaims");
builder.Entity<IdentityRole>().ForSqlServerToTable("Roles");
}
}

Joseph Bailey
- 81
- 6
-
worked for me with plain old `ToTable` instead of `ForSqlServerToTable`. (though i was using asp.net core identity with dotnet core sample project so, might not help you) – Aug 13 '16 at 05:59