【发布时间】:2019-02-06 01:32:18
【问题描述】:
我正在 VS2017 v15.8.2 中开发一个 ASP.Net Core 2.1.1 Web 应用程序,使用带有个人用户帐户的 Web 应用程序模板。
我想向 IdentityUser 添加更多属性,因此我创建了一个继承自 IdentityUser 的 ApplicationUser 类。我对 startup.cs 和 _loginPartial.cshtml 应用了适当的更新。一切都可以编译,但是当我运行Add-Migration 时,它不会获取我的ApplicationUser 类中的属性。我有一个迁移类,但是 up 和 down 方法没有属性。
我只有一个项目,所以在包管理器控制台中选择默认项目不是问题。
这是我的 ApplicationUser 类
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
public string Street { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zipcode { get; set; }
public int CompanyId { get; set; }
}
这是我的 DBContext
public class ApplicationDbContext : IdentityDbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
这里是Startup.cs中的服务,ConfigureService
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.
GetConnectionString("AccountDbConnectionString")));
services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
这是包管理器控制台中的添加迁移
Add-Migration -c ApplicationDbContext V1.00.01
这是最终的迁移
public partial class V10001 : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
ApplicationUser 类位于项目根目录下的 Models 文件夹中。
IdentityUser 在我第一次创建项目时被解析为初始 Asp.Net Core Web 应用程序模板迁移,当我运行 update-database 命令时,它在数据库中创建了所有 AspNetUserxxx 和 AspNetRolexxx 表就好了.
但我不明白为什么 EF Migrations 没有看到 ApplicationUser 类,因此它可以将新属性添加到迁移类中。
当我在网上查找示例时,看起来我所做的一切都正确地扩展了 IdentyUser 属性。
我错过了什么?
编辑 1:我在这篇 SO 帖子中尝试了解决方案,但没有任何区别。 Entity Framework Core 2.0 add-migration not generating anything
【问题讨论】:
标签: asp.net-identity entity-framework-core asp.net-core-2.0 asp.net-core-2.1 ef-core-2.1