Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.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
Entity framework 使用TPT和鉴别器的Entityframework 4.3 codefirst_Entity Framework_Ef Code First_Table Per Type_Discriminator - Fatal编程技术网

Entity framework 使用TPT和鉴别器的Entityframework 4.3 codefirst

Entity framework 使用TPT和鉴别器的Entityframework 4.3 codefirst,entity-framework,ef-code-first,table-per-type,discriminator,Entity Framework,Ef Code First,Table Per Type,Discriminator,我有以下三种型号: public class Equipment { public int ID { get; set; } public string title { get; set; } } [Table("Vessels")] public class Vessel:Equipment { public string Size { get; set; } } [Table("Tubes")] public class Tube : Equipment {

我有以下三种型号:

public class Equipment
{
    public int ID { get; set; }
    public string title { get; set; }

}

[Table("Vessels")]
public class Vessel:Equipment
{
    public string Size { get; set; }

}

[Table("Tubes")]
public class Tube : Equipment
{
    public string Pressure{ get; set; }

}
我想显示一个设备清单,有两列标题和类型

例如:

Title        Type
------       -------
101-1        vessel
101-2        vessel
102-3        tube
我不知道如何在设备中制作一个鉴别器列来显示每个设备的类型

已编辑

如果我在设备实体中有一个鉴别器,如:

public class Equipment
{
    public int ID { get; set; }
    public string title { get; set; }
    public string type{ get; set; }  //as discriminator
}
我可以在controller或存储库中获得如下查询:

var equipments=from e in db.Equipments
               select e;

不能根据EF映射生成鉴别器列-TPT继承不支持它,因为鉴别器是子表。您可以尝试使用以下方法:

public abstract class Equipment
{
    public int ID { get; set; }
    public string title { get; set; }

    [NotMapped]
    public abstract string Type { get; }
}  

并重写子类型中的
Type
属性以获得正确的名称。您将无法在Linq to Entities查询中使用该属性,因为它未映射。

感谢您的回复,我已经阅读了您在本文中的评论,这两个评论都很好,也很有帮助,但我不明白如何从我的dbcontext中导出该查询。我将编辑我的问题,请看一下问题的结尾。您上次的查询是如何依赖于鉴别器的?非常感谢,我想我知道我的错误在哪里。我想让类型(鉴别器)像TPH一样自动填充,但现在我明白了,我必须在代码中设置值,我将从容器和管道中获得每个实例。非常感谢@LadislavMrnka