Mysql 实体类型';IdentityUser';是层次结构的一部分,但未配置鉴别器值

Mysql 实体类型';IdentityUser';是层次结构的一部分,但未配置鉴别器值,mysql,asp.net-core,entity-framework-core,mysql-connector,Mysql,Asp.net Core,Entity Framework Core,Mysql Connector,我正在尝试使用EntityFrameworkCore(1.0.1)将.NET Core 1.1 MVC应用程序从SQLite(“Microsoft.EntityFrameworkCore.SQLite:“1.0.1”)迁移到MySQL(“MySQL.Data.EntityFrameworkCore:“7.0.6-IR31”) 我的上下文正在扩展IdentityDbContext,当运行Database.EnsureCreated(),使用Sqlite时,会自动配置AspNet表(RoleClai

我正在尝试使用EntityFrameworkCore(1.0.1)将.NET Core 1.1 MVC应用程序从SQLite(
“Microsoft.EntityFrameworkCore.SQLite:“1.0.1”
)迁移到MySQL(
“MySQL.Data.EntityFrameworkCore:“7.0.6-IR31”

我的上下文正在扩展
IdentityDbContext
,当运行
Database.EnsureCreated()
,使用Sqlite时,会自动配置AspNet表(RoleClaims、Roles、Users等)

使用MySQL连接器,当我尝试运行
updatedatabase
Database.EnsureCreated()
时,会出现以下错误:

实体类型“IdentityUser”是层次结构的一部分,但未配置鉴别器值。

我真的应该手动配置吗?如果是,如何配置

背景:

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);
    }
}

EntityFramework核心的MySQL提供程序似乎不会自动注册派生类(其他提供程序会隐式注册),这就是为什么需要在代码中指定鉴别器的原因

但是,由于
IdentityUser
不是一个抽象类,因此理论上它也可以实例化和分配,MySQL数据库提供程序也需要注册它(即使在生产中,这个鉴别器永远不会有值)

所以你也必须注册基类

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<IdentityUser>()
        .HasDiscriminator<int>("Type")
        .HasValue<IdentityUser>(0)
        .HasValue<ApplicationUser>(1);
}
模型创建时受保护的覆盖无效(ModelBuilder)
{
基于模型创建(生成器);
builder.Entity()
.HasDiscriminator(“类型”)
.HasValue(0)
.HasValue(1);
}

显示您的身份模型(通常称为ApplicationUser、ApplicationDbContext等)。dbcontext中要么有基类和派生类,要么使用了错误的基类class@Tseng我已经更新了我的帖子。让我澄清一下——使用SQLite,我让一切都正常工作,并且生成了AspNet表并可用。在我的上下文中,除了在OnModelCreating中添加discriminator之外,我没有做任何更改,尽管在SQLite中它自动添加discriminator列,并在该列中将所有用户设置为“ApplicationUser”。您能详细说明一下“您的dbcontext中要么有基类,要么有派生类”吗@TsengI怀疑您的上下文中可能有
DbSet
DbSet
,但我看不出来,因为这可能会导致这样的错误,因为您的代码只为基类(IdentityUser)添加了一个鉴别器,而不为ApplicationUser添加鉴别器。Oracles MySQL提供程序并不是最好的/最完善的提供程序之一,并且仍在开发中,(至少)官方MySQL提供程序会抱怨,如果您没有注册基类(即,在上面的代码中添加
.HasValue(0)
,以获得提供程序列表。Postresql在早期rc1版本中可用,因此它应该很好(npgsql)如果你真的有,也可以选择一个mysql提供商(付费或免费)。但是首先你可以尝试添加
.HasValue(0)
,看看它是否有效。你对“抽象”类的评论让我意识到我忘了标记我的基类。谢谢。这里也是。哦。
protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    builder.Entity<IdentityUser>()
        .HasDiscriminator<int>("Type")
        .HasValue<IdentityUser>(0)
        .HasValue<ApplicationUser>(1);
}