EF7迁移-实体类型“"对应的CLR类型不可实例化 [英] EF7 Migrations - The corresponding CLR type for entity type '' is not instantiable

查看:83
本文介绍了EF7迁移-实体类型“"对应的CLR类型不可实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用EF7迁移,并在通过继承对 Organization 组织进行建模时陷入困境.

I'm trying to use EF7 migrations and got stuck when I modeled an Organization model with inheritance.

组织是抽象类.从其继承的两个具体类分别称为 Individual Company .

Organization is an abstract class. There are two concrete classes that inherit from it called Individual and Company.

我在 DbContext 中将 Organization 抽象类设置为 DbSet< Organization> 并运行迁移.

I set the Organization abstract class as DbSet<Organization> in DbContext and run migrations.

我正在按照本教程显示以下错误:

实体类型组织"的对应CLR类型不可实例化,并且模型中没有与特定CLR类型相对应的派生实体类型.

The corresponding CLR type for entity type 'Organization' is not instantiable and there is no derived entity type in the model that corresponds to a concrete CLR type.

我该怎么办?

编辑-已更新代码.

组织:

public abstract class Organization
{
    public Organization()
    {
        ChildOrganizations = new HashSet<Organization>();
    }

    [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public bool Enabled { get; set; }
    public bool PaymentNode { get; set; }
    public DateTime Created { get; set; }
    public DateTime Updated { get; set; }

    // virtual
    public virtual ICollection<Organization> ChildOrganizations { get; set; }
}

个人

public class Individual : Organization
{
    public string SocialSecurityNumber { get; set; }
    public string Firstname { get; set; }
    public string Lastname { get; set; }
}

公司

public class Company : Organization
{
    public string Name { get; set; }
    public string OrganizationNumber { get; set; }
}

DbContext

public class CoreDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Organization> Organization { get; set; }

    public CoreDbContext(DbContextOptions<CoreDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

提前谢谢!

推荐答案

请参阅:

如果您不想为层次结构中的一个或多个实体公开DbSet,则可以使用Fluent API来确保它们包含在模型中.

If you don’t want to expose a DbSet for one or more entities in the hierarchy, you can use the Fluent API to ensure they are included in the model.

如果不想为每个子类创建一个 DbSet ,则必须在 DbContext <的 OnModelCreating 替代中显式定义它们./code>:

If you don't want to have to create a DbSet for each subclass then you have to explicitly define them in the OnModelCreating override of the DbContext:

public class CoreDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Organization> Organization { get; set; }

    public CoreDbContext(DbContextOptions<CoreDbContext> options)
        : base(options)
    {
    }

    protected override void OnModelCreating(ModelBuilder builder)
    {
        builder.Entity<Individual>();
        builder.Entity<Company>();

        base.OnModelCreating(builder);
        // Customize the ASP.NET Identity model and override the defaults if needed.
        // For example, you can rename the ASP.NET Identity table names and more.
        // Add your customizations after calling base.OnModelCreating(builder);
    }
}

这篇关于EF7迁移-实体类型“"对应的CLR类型不可实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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