EF7遷移 - 實體類型''的相應CLR類型不可實例化

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

我正在嘗試使用EF7遷移,並在使用繼承建模組織模型時遇到困難。

組織是一個抽像類,有兩個繼承它的具體類叫做Individual和Company。

我在DbContext中將組織抽像類設置為DbSet並運行遷移。

我在這裡遵循這個教程

顯示以下錯誤:

實體類型“組織”的相應CLR類型不可實例化,並且模型中沒有與具體CLR類型對應的派生實體類型。

我該怎麼辦?

編輯 - 更新代碼

組織

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

提前致謝!

一般承認的答案

請參閱: https//docs.microsoft.com/en-us/ef/core/modeling/inheritance

如果您不想為層次結構中的一個或多個實體公開DbSet,則可以使用Fluent API確保它們包含在模型中。

如果你不希望有創建DbSet對於每個子類,那麼你必須在明確定義它們OnModelCreating中的覆蓋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);
    }
}

熱門答案

與您鏈接的教程類似,您的DbSet<>屬性應該是繼承的IndividualCompany類。

嘗試讓你的CoreDbContext看起來更像這樣:

public class CoreDbContext : IdentityDbContext<ApplicationUser>
{
    public DbSet<Company> Companies { get; set; }
    public DbSet<Individual> Individuals { 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);
    }
}


Related

許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因