asp.net标识2中用于更改标识表名称的方法在asp.net标识3中不起作用。
您可以通过在DbContext
:的OnModelCreating
上使用扩展方法ToTable("TableName")
更改实体映射来轻松完成此操作
并且您不需要使用.ForSqlServerToTable()
,只要.ToTable()
就可以在任何数据库中工作。
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");
}
这里唯一的问题是记住使用具有标识符类型的泛型(字符串是AspNetCore.上的默认值)
在ApplicationDbContext的OnModelCreating中修改构建器实体,使用ForSqlServerToTable扩展方法更改所需的表名。
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");
}
}