我想更改身份表名称 . 我用Google搜索并找到了以下方法:
第一个:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>(entity =>
{
entity.ToTable(name: "Users");
});
builder.Entity<ApplicationRole>(entity =>
{
entity.ToTable(name: "Roles");
});
builder.Entity<ApplicationRoleClaim>(entity =>
{
entity.ToTable(name: "RoleClaims");
});
builder.Entity<ApplicationUserRole>(entity =>
{
entity.ToTable(name: "UserRoles");
});
builder.Entity<ApplicationUserLogin>(entity =>
{
entity.ToTable(name: "UserLogins");
});
builder.Entity<ApplicationUserClaim>(entity =>
{
entity.ToTable(name: "UserClaims");
});
builder.Entity<ApplicationUserToken>(entity =>
{
entity.ToTable(name: "UserTokens");
});
}
第二个:
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<ApplicationUser>().ToTable("Users");
builder.Entity<ApplicationRole>().ToTable("Roles");
builder.Entity<ApplicationRoleClaim>().ToTable("RoleClaims");
builder.Entity<ApplicationUserRole>().ToTable("UserRoles");
builder.Entity<ApplicationUserClaim>().ToTable("UserClaims");
builder.Entity<ApplicationUserToken>().ToTable("UserTokens");
builder.Entity<ApplicationUserLogin>().ToTable("UserLogins");
}
所有通用名称,如ApplicationUser,ApplicationRole ......都有 int 作为主键 .
ApplicationDbContext 和 StartUp 如下所示
ApplicationDbContext
public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, int>
StartUp class
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(Configuration.GetConnectionString("MySQLConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
如果你注意到,在启动课上,我没有在 AddEntityFrameworkStores<ApplicationDbContext>().
上添加 int 因为,编译错误一直显示 "Not supported....".
我执行了以下操作来删除旧数据库和迁移
$ drop-database
$ remove-migration
添加新配置
$ add-migration Initial
$ update-database
我发现的结果是,只有用户和角色表被更改,而其他人没有(AspUserRoles,AspUserClaim .....) .
FYI:
我正在使用Visual Studio 2017.我的项目使用默认的Web应用程序和个人用户帐户选择和 .NET Core 2. 我也使用 Pomela.EntityFramework.MySql 用于数据库提供程序 .
My Question is: 我做错了什么?或者改变了什么?
1 回答
您需要告诉
IdentityDbContext
您的所有自定义类型 . 为此,您需要将传递的泛型扩展为IdentityDbContext
,如下所示: