7

我正在尝试在实体框架模型中急切加载派生类的属性。

我到处读到我必须先用 OfType() 过滤集合,然后再用 Include() 包含属性

var persons = Context.Persons
                     .OfType<Employee>()
                     .Include("Compensation")

我不知道如何让 Include() 工作,因为在我的情况下,Persons 是一个 DbSet,OfType() 返回一个 IQueryable,而 IQueryable 没有定义 Include() 方法。

4

1 回答 1

16

放置这个:

using System.Data.Entity;

进入您的使用列表,之后您将能够使用类中的Include扩展方法系列DbExtensions

    public static IQueryable<T> Include<T, TProperty>(this IQueryable<T> source, Expression<Func<T, TProperty>> path) where T : class;
    public static IQueryable<T> Include<T>(this IQueryable<T> source, string path) where T : class;
    public static IQueryable Include(this IQueryable source, string path);

他们接受 IQueryable 作为第一个参数,也有强类型的,那就更好了Include(String)

于 2012-08-23T05:52:08.787 回答