[英]How to Rename AspNetUsers table for a class inherited from IdentityUser in ASP .NET Core 2.1?
I'm using ASP .NET Core 2.1 for my project with IndividualAuthentication. 我在使用PersonalAuthentication的项目中使用ASP .NET Core 2.1。 I need extra properties for my User table so i inherited from
IdentityUser
like below : 我的User表需要额外的属性,所以我继承了
IdentityUser
如下所示:
public class ApplicationUser : IdentityUser
{
[Required]
[DataType(DataType.Text)]
public string Name { get; set; }
[Required]
[DataType(DataType.Text)]
public string LastName { get; set; }
}
After this modification AspNetUsers
table is not renamed. 此修改后,
AspNetUsers
表未重命名。 All other identity tables are renamed. 重命名所有其他标识表。 I don't know why is this happening.
我不知道为什么会这样。
After creating ApplicationUser
class i have replaced IdentityUser
with ApplicationUser
in my code of Startup.cs
在创建
ApplicationUser
类之后,我已经在我的Startup.cs
代码中用ApplicationUser
替换了IdentityUser
Below is the code before Modification 以下是修改前的代码
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<IdentityUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
After modification 修改后
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
This is my OnModelCreating method 这是我的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");
}
Now i don't know what other thing i am missing for renaming AspNetUsers
table. 现在我不知道在重命名
AspNetUsers
表时我还缺少什么。 I didn't find any solution and still searching. 我没有找到任何解决方案,仍然在寻找。
The fluent configuration is ok, but the identity class used as base for your context is not. 流畅的配置是可以的,但用作上下文基础的标识类不是。
As explained in the Customizing the model (emphasis is mine): 正如定制模型中所解释的那样(重点是我的):
The starting point for customizing the model is to derive from the appropriate context type ;
自定义模型的起点是从适当的上下文类型派生 ; see the preceding section.
见上一节。
and the preceding sections explain the base classes, generic type arguments and the default configuration. 前面的部分解释了基类,泛型类型参数和默认配置。
With that being said, since you are using only a custom IdentityUser
derived class, the base at least should be the IdentityDbContext<TUser>
: 话虽如此,因为您只使用自定义的
IdentityUser
派生类,所以基本至少应该是IdentityDbContext<TUser>
:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.