【发布时间】:2018-12-29 12:46:20
【问题描述】:
我在我的项目中使用 ASP .NET Core 2.1 和 IndividualAuthentication。我的 User 表需要额外的属性,所以我继承自 IdentityUser,如下所示:
public class ApplicationUser : IdentityUser
{
[Required]
[DataType(DataType.Text)]
public string Name { get; set; }
[Required]
[DataType(DataType.Text)]
public string LastName { get; set; }
}
修改后AspNetUsers 表没有重命名。所有其他身份表都被重命名。我不知道为什么会这样。
创建ApplicationUser 类后,我在Startup.cs 的代码中将IdentityUser 替换为ApplicationUser
下面是修改前的代码
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 表还缺少什么。我没有找到任何解决方案,仍在寻找。
【问题讨论】:
-
很可能您没有使用正确的基本泛型
IdentityDbContext。我们能看到吗,例如class ApplicationDbContext : IdentityDbContext<…>? -
public class ApplicationDbContext : IdentityDbContext { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } }
标签: c# entity-framework-migrations asp.net-core-identity asp.net-core-2.1 ef-core-2.1