【问题标题】:Eagerly loading subtype specific entities急切地加载特定于子类型的实体
【发布时间】:2011-06-25 12:20:16
【问题描述】:

SubFoo 是 Foo 的子类型,但只有 Bar 显示导航属性 Bar。所以

DB.Foos.Include('Bar')

生成

指定的包含路径无效。 EntityType 'Foo' 没有声明名为 'Bar' 的导航属性。

我该怎么办? (除了显然将 Bar 移到 Foo 之外)

【问题讨论】:

    标签: asp.net-mvc-2 entity-framework-4


    【解决方案1】:

    怎么样:

    DB.Foos.OfType<SubFoo>().Include("Bar")
    

    如果您想对 Foos(不仅是 SubFoos)进行一般查询,并且所有 SubFoos 都必须加载 Bar,则此方法不起作用..

    【讨论】:

      【解决方案2】:

      一种方法是投影

      var q = from f in DB.Foos
              let b = (f as SubFoo).Bar
              select new
              {
                  Foo = f,
                  Bar = b
              };
       return q.AsEnumerable().Select(q => q.Foo);
      

      【讨论】:

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