簡體   English   中英

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

[英]EF7 Migrations - The corresponding CLR type for entity type '' is not instantiable

我正在嘗試使用 EF7 遷移,但在使用繼承對Organization模型進行建模時卡住了。

Organization是一個抽象類。 有兩個具體的類繼承自它,稱為IndividualCompany

我在DbContext中將Organization抽象類設置為DbSet<Organization>並運行遷移。

我在這里關注本教程。

顯示以下錯誤:

實體類型“組織”的對應 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; }
}

數據庫上下文

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

萬一有人像我一樣犯了一個愚蠢的錯誤,我的實體是抽象的。 所以也許檢查你是否錯誤地提出了同樣的問題。

問題可能是您使用了class library項目。 在 EF Core 的 RC2 中,您不能將 DBContext 放在這樣的項目中。 這是一個已知問題。 當您將其轉換為“應用程序”項目時,它應該會再次運行。

更多信息和來源:

解決方法: https : //docs.efproject.net/en/latest/cli/dotnet.html#dotnet-cli-issues

Github 問題: https : //github.com/aspnet/EntityFramework/issues/5460

您還應該在CoreDbContext類中放置繼承 Organization 的類。 然后它將正確區分和實例化這些對象。 它應該是這樣的:

public DbSet<Organization> Organization { get; set; }
public DbSet<Individual> Individual { get; set; }
public DbSet<Company> Company { get; set; }

來源: https : //docs.microsoft.com/en-us/ef/core/modeling/inheritance

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM