实体类型"IdentityUser"是层次结构的一部分,但未配置识别符值 [英] The entity type 'IdentityUser' is part of a hierarchy, but does not have a discriminator value configured

查看:191
本文介绍了实体类型"IdentityUser"是层次结构的一部分,但未配置识别符值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to migrate a .NET Core 1.1 MVC-application using EntityFrameworkCore (1.0.1) from SQLite ("Microsoft.EntityFrameworkCore.Sqlite": "1.0.1") to MySQL ("MySql.Data.EntityFrameworkCore": "7.0.6-IR31").

我的上下文正在扩展IdentityDbContext,并且在运行Database.EnsureCreated()时,使用Sqlite会自动配置AspNet表(RoleClaims,Role,User等).

My context is extending IdentityDbContext, and when running Database.EnsureCreated(), using Sqlite, the AspNet-tables (RoleClaims, Roles, Users, etc) were automatically configured.

使用MySQL连接器,当我尝试运行Update-DatabaseDatabase.EnsureCreated()时,出现错误:

With the MySQL-connector, when i attempt to run either Update-Database or Database.EnsureCreated(), I get the error:

The entity type 'IdentityUser' is part of a hierarchy, but does not have a discriminator value configured.

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

Should I really configure this manually, and if so, how?

上下文:

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

ApplicationUser:

ApplicationUser:

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 Provider不会自动为派生类注册(其他提供商隐式注册),​​这就是为什么您需要指定代码中的区分符.

It seems that the MySQL Provider for EntityFramework Core doesn't automatically do registrations for derived classes (which other providers do implicitly), which is why you need to specify the discriminators in code.

但是,由于IdentityUser不是抽象类,因此理论上也可以实例化和分配它,并且MySQL数据库提供程序也需要对其进行注册(即使在生产中,使用该鉴别符也永远不会有值) .

However, since IdentityUser is not an abstract class, it can in theory be instantiated and assigned too and the MySQL Database Provider requires a registration for it too (even if in production there will never be an value with this discriminator).

因此,您也必须注册基类.

So you have to register the base class too.

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

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

这篇关于实体类型"IdentityUser"是层次结构的一部分,但未配置识别符值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆