2016-05-23 55 views
8

我試圖使用EF7遷移,並在繼承模型化了組織模型時陷入了困境。EF7遷移 - 實體類型''的相應CLR類型不可實例化

組織是一個抽象類,有兩個具體類繼承它,稱爲個人和公司。

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

我下面這個教程顯示here

以下錯誤:

爲實體型「組織」的對應的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); 
    } 
} 

提前感謝!

+0

請在問題中添加代碼。 –

+0

您是否在同一個項目或獨立項目中擁有DbContext? –

+0

@TomDroste DbContext與域模型在同一個類庫項目中,遷移在Web項目中。 – Rovdjuret

回答

11

請參閱: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); 
    } 
} 
+0

不錯,我會試試看! – Rovdjuret

+0

我遇到了這個問題,你的答案解決了我的問題。因爲這是最合乎邏輯的用例(爲什麼即使有基類,如果你只會查詢子類?)我認爲這個答案應該是可以接受的。 – blagae

+0

您的鏈接已被打破。 –

0

問題可能是您使用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

+1

情況並非如此。過去我曾經遇到過這個問題,並將其解讀爲您發佈的鏈接。我已經成功地使用了遷移,但是現在當我使用繼承時,它會引發我在前面描述中提供的錯誤。這似乎是問題與TPT(表每種類型)請參閱鏈接:http://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-2- table-per-type-tpt – Rovdjuret

1

類似於你鏈接的教程,你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); 
    } 
} 
+3

在EF6中,根據此:http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/implementing-inheritance-with -the-entity-framework-in-an-asp-net-mvc-application 你可以使用基類定義DbSet。在EF Core中做同樣的事情似乎不起作用。 – jcmcbeth