问题
The method used in asp.net identity 2 to alter the identity table names does not work in asp.net identity 3.
回答1:
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.
回答2:
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");
}
}
来源:https://stackoverflow.com/questions/34523066/how-can-i-change-the-table-names-used-by-asp-net-identity-3-vnext