如何为从 ASP .NET Core 2.1 中的 IdentityUser 继承的 class 重命名 AspNetUsers table?
How to Rename AspNetUsers table for a class inherited from IdentityUser in ASP .NET Core 2.1?
我正在为我的项目使用 ASP .NET Core 2.1 和 IndividualAuthentication。我的用户 table 需要额外的属性,所以我继承自 IdentityUser
如下所示:
public class ApplicationUser : IdentityUser
{
[Required]
[DataType(DataType.Text)]
public string Name { get; set; }
[Required]
[DataType(DataType.Text)]
public string LastName { get; set; }
}
本次修改后AspNetUsers
table未改名。所有其他身份 table 均已重命名。我不知道为什么会这样。
创建 ApplicationUser
class 后,我在 Startup.cs
的代码中用 ApplicationUser
替换了 IdentityUser
以下是修改前的代码
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
修改后
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
这是我的 OnModelCreating 方法
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
base.OnModelCreating(builder);
modelBuilder.Entity<ApplicationUser>().ToTable("User");
//modelBuilder.Entity<IdentityUser>().ToTable("User");
modelBuilder.Entity<IdentityRole>().ToTable("Role");
modelBuilder.Entity<IdentityUserClaim<string>().ToTable("UserClaim");
modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
modelBuilder.Entity<IdentityUserLogin<string>().ToTable("UserLogin");
modelBuilder.Entity<IdentityRoleClaim<string>().ToTable("RoleClaim");
modelBuilder.Entity<IdentityUserToken<string>().ToTable("UserToken");
}
现在我不知道重命名还缺少什么 AspNetUsers
table。我没有找到任何解决方案,仍在搜索中。
流畅的配置没问题,但用作上下文基础的标识 class 却不行。
如自定义模型中所述
(重点是我的):
The starting point for customizing the model is to derive from the appropriate context type; see the preceding section.
前面的部分解释了基础 classes、泛型类型参数和默认配置。
话虽这么说,因为您只使用自定义 IdentityUser
派生的 class,基础至少应该是 IdentityDbContext<TUser>
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
// ...
}
我正在为我的项目使用 ASP .NET Core 2.1 和 IndividualAuthentication。我的用户 table 需要额外的属性,所以我继承自 IdentityUser
如下所示:
public class ApplicationUser : IdentityUser
{
[Required]
[DataType(DataType.Text)]
public string Name { get; set; }
[Required]
[DataType(DataType.Text)]
public string LastName { get; set; }
}
本次修改后AspNetUsers
table未改名。所有其他身份 table 均已重命名。我不知道为什么会这样。
创建 ApplicationUser
class 后,我在 Startup.cs
ApplicationUser
替换了 IdentityUser
以下是修改前的代码
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
修改后
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
这是我的 OnModelCreating 方法
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
base.OnModelCreating(builder);
modelBuilder.Entity<ApplicationUser>().ToTable("User");
//modelBuilder.Entity<IdentityUser>().ToTable("User");
modelBuilder.Entity<IdentityRole>().ToTable("Role");
modelBuilder.Entity<IdentityUserClaim<string>().ToTable("UserClaim");
modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
modelBuilder.Entity<IdentityUserLogin<string>().ToTable("UserLogin");
modelBuilder.Entity<IdentityRoleClaim<string>().ToTable("RoleClaim");
modelBuilder.Entity<IdentityUserToken<string>().ToTable("UserToken");
}
现在我不知道重命名还缺少什么 AspNetUsers
table。我没有找到任何解决方案,仍在搜索中。
流畅的配置没问题,但用作上下文基础的标识 class 却不行。
如自定义模型中所述 (重点是我的):
The starting point for customizing the model is to derive from the appropriate context type; see the preceding section.
前面的部分解释了基础 classes、泛型类型参数和默认配置。
话虽这么说,因为您只使用自定义 IdentityUser
派生的 class,基础至少应该是 IdentityDbContext<TUser>
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
// ...
}