我正在尝试使用entityFrameWorkCore(1.0.1)从SQLITE("Microsoft.EntityFrameworkCore.Sqlite": "1.0.1"
)迁移.NET Core 1.1 MVC-Application到MySQL("MySql.Data.EntityFrameworkCore": "7.0.6-IR31"
)。
我的上下文正在扩展IdentityDbContext
,并且在运行Database.EnsureCreated()
时,使用SQLITE,ASPNET-TABLE(RoleReclaim,角色,用户等)被自动配置。
使用MySQL-Connector,当我尝试运行Update-Database
或Database.EnsureCreated()
时,我会得到错误:
The entity type 'IdentityUser' is part of a hierarchy, but does not have a discriminator value configured.
我应该真的手动配置这个,如果是,如何?
上下文:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
//DbSets here..
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<IdentityUser>()
.HasDiscriminator<int>("Type")
.HasValue<ApplicationUser>(1);
}
}
应用程序器:
public class ApplicationUser : IdentityUser
{
public string Firstname { get; set; }
public string Lastname { get; set; }
public string Firmname { get; set; }
public string CVR { get; set; }
public string ATT { get; set; }
public string Telephone { get; set; }
public string Address { get; set; }
}
看来,EntityFramework Core的MySQL提供商不会自动对派生类进行注册(其他提供商隐含地使用),这就是为什么您需要在代码中指定歧视器的原因。
但是,由于IdentityUser
不是抽象类,因此在理论上也可以实例化和分配,并且MySQL数据库提供商也需要对其进行注册(即使在生产中,这种歧视者也永远不会有一个值)。
因此,您也必须注册基类。
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
builder.Entity<IdentityUser>()
.HasDiscriminator<int>("Type")
.HasValue<IdentityUser>(0)
.HasValue<ApplicationUser>(1);
}