实体框架:根据子类型包含属性

时间:2016-05-03 09:08:04

标签: c# entity-framework

我有一个名为' Organization'的Main Model-Type,它有多个Subtypes。在Entity Framework中,我们通过Table per Type appraoch保存它。 现在,一些Subtypes还有其相关信息的NavigationProperties,我想加载它,具体取决于Type。 问题是:我无法定义具体的组织类型DbSet,因为在我不得不知道的时候我不知道这个。 所以基本上,我需要告诉EF:如果您加载此类型的组织,包括SubType1,如果它是另一个Type,SubType2等。

如果我做这个传统,EF会因为Type" Organization"而呻吟。我不知道子类型的导航属性。

是否有可能将EF多元化与包含特定表格的可能性结合起来?

1 个答案:

答案 0 :(得分:0)

只需在dbcontext中添加它,您就可以在子类型中定义任何信息,只需确保您至少使用EF 6.0

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            //inhertance table per type 
            modelBuilder.Entity<Reponse>().ToTable("Organisation");
            modelBuilder.Entity<ReponseChoix>().ToTable("Subtype1");
            modelBuilder.Entity<ReponseSimple>().ToTable("Subtype2");


        }
相关问题