【问题标题】:EF TPH Inheritance QueryEF TPH 继承查询
【发布时间】:2011-01-02 13:36:02
【问题描述】:

尝试为我正在制作的系统实现一个非常简单的 TPH 设置,1 个基类,2 个继承类。

但是继承的类都属于同一个实体集,所以在我的 ObjectContext using loop 中,我只能访问基本抽象类。我不太确定如何获得具体类的元素? (我也将其转换为使用 POCO)。

然后在我的应用程序中使用实体:

using (SolEntities sec = new SolEntities()) {
    Planets = sec.CelestialBodies;
}

sec 上设置了 CelestialBodies 实体,但没有我期望的行星/卫星。

不太清楚需要做什么才能访问它们。

谢谢

【问题讨论】:

    标签: c# entity-framework c#-4.0 tph


    【解决方案1】:

    您可以使用OfType 方法:

    using (SolEntities sec = new SolEntities()) {
        Planets = sec.CelestialBodies.OfType<Planet>();
    }
    

    【讨论】:

      【解决方案2】:

      正如 Thomas Levesque 所述,OfType 扩展方法将允许您仅查询您真正想要访问的单个继承类型。如果您直接访问CelestialBodies,您将获得所有实体。每个实体的类型都是 PlanetSatellite,但您必须强制转换它们才能访问它们的属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多