c# - EF7 迁移 - 实体类型 '' 对应的 CLR 类型不可实例化

标签 c# entity-framework asp.net-core entity-framework-core

我正在尝试使用 EF7 迁移,但在为 Organization 建模时卡住了具有继承性的模型。

Organization是一个抽象类。有两个继承自它的具体类 IndividualCompany .

我设置了 Organization抽象类为 DbSet<Organization>DbContext并运行迁移。

我正在学习本教程 here .

显示以下错误:

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

数据库上下文

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

提前致谢!

最佳答案

请参阅:https://learn.microsoft.com/en-us/ef/core/modeling/inheritance

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 重写中显式定义它们:

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

关于c# - EF7 迁移 - 实体类型 '' 对应的 CLR 类型不可实例化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37398141/

相关文章:

c#如何获取当前可执行文件.exe的版本

c# - 为什么 LinkedList 通常比 List 慢?

c# - 从单独的列在 Entity Framework 中添加日期时间和时间

c# - 关于更新断开实体图的思考

entity-framework - 如果使用 Entity Framework ,如何获取上次插入的 ID

c# - .NET Core 从项目中排除一个类

c# - ASP.NET Core 3.1 URL 链接到需要授权的页面,给出错误防伪 token 验证失败

c# - 如何在 C# 中更改 DataGridViewComboBoxColumn 的行为?

c# - SQL 连接和事务

c# - 如何在 EF Core 3 中启用日志记录?