【发布时间】:2018-02-17 12:35:51
【问题描述】:
我想更改身份表名称。我搜索了一下,发现了以下方法:
第一个:
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>
启动类
services.AddDbContext<ApplicationDbContext>(options =>
options.UseMySql(Configuration.GetConnectionString("MySQLConnection")));
services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
如果您注意到,在启动类中,我没有在 AddEntityFrameworkStores<ApplicationDbContext>(). 上添加 int 因为,编译器错误一直显示为 “不支持...。 "。
我做了以下删除旧数据库和迁移
$ drop-database
$ remove-migration
添加新配置
$ add-migration Initial
$ update-database
我发现的结果是,只有用户和角色表被改变,而其他的没有改变(AspUserRoles,AspUserClaim.....)。
仅供参考:
我正在使用 Visual Studio 2017。我的项目使用默认的 Web 应用程序,并选择了 个人用户帐户 和 .NET Core 2。 我还使用 Pomela.EntityFramework.MySql 作为数据库提供者。
我的问题是:我做错了什么?或者发生了什么变化?
【问题讨论】:
标签: c# asp.net entity-framework asp.net-core asp.net-identity