Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体框架一个多TPH映射_C#_Entity Framework_Ef Code First_Entity Framework 6 - Fatal编程技术网

C# 实体框架一个多TPH映射

C# 实体框架一个多TPH映射,c#,entity-framework,ef-code-first,entity-framework-6,C#,Entity Framework,Ef Code First,Entity Framework 6,我使用的数据结构与此类似,其中动物类型由表中的鉴别器列确定: public class Farm { public int Id { get; set; } public virtual ICollection<Pig> Pigs { get; set; } public virtual ICollection<Cow> Cows { get; set; } } public class Animal { public int Id

我使用的数据结构与此类似,其中动物类型由表中的鉴别器列确定:

public class Farm {
    public int Id { get; set; }

    public virtual ICollection<Pig> Pigs { get; set; }

    public virtual ICollection<Cow> Cows { get; set; }
}

public class Animal {
    public int Id { get; set; }

    public int FarmId? { get; set; }

    public virtual Farm Farm { get; set; }

    public string Name { get; set; }
}

public class Pig : Animal {}
public class Cow : Animal {}
来自每种动物的映射也不起作用,它会生成额外的列(例如,
Farm\u Id
Farm\u Id 1
——除了
FarmId
——每种动物类型一列)

将导航属性从
Animal
模型移动到继承模型会导致生成一个额外的列-
FarmId1
(因此比上面的2更接近我想要的内容!)


有什么办法可以做到这一点吗?

我不是EF专家,但从模型优先的方法中,我知道这将映射为一个动物集合,然后您可以选择
Farm.Animals.OfType()

这难道不意味着整个动物集合都是从SQL Server加载的,然后过滤到猪的集合中吗?(这正是我在这个问题上试图避免的…)你有没有为你的情况找到任何解决方案,这就是我一直在寻找的。。
this.Map<Pig>(m => m.Requires("Type").HasValue((int) AnimalType.Pig));
this.Map<Cow>(m => m.Requires("Type").HasValue((int) AnimalType.Cow));
this.HasMany(t => t.Pigs)
    .WithOptional(t => t.Farm)
    .Map(m => m.MapKey("FarmId"));
this.HasMany(t => t.Cows)
    .WithOptional(t => t.Farm)
    .Map(m => m.MapKey("FarmId"));
this.HasOptional(t => t.Farm)
    .WithMany(t => t.Pigs)
    .HasForeignKey(d => d.FarmId)