【发布时间】:2010-12-02 05:35:07
【问题描述】:
我首先使用的是 EF4 CPT4 代码,并且我已经设置了我的 ObjectContext 以返回 IObjectSet,这样我就可以模拟和测试我的 repos。但是,我现在注意到我无法像使用 ObjectSet 那样使用 .Include() 方法进行预加载。
有没有办法让它工作???
编辑: 我添加了这个扩展方法:
public static IQueryable<TSource> Include<TSource>(this IQueryable<TSource> source, string path)
{
var objectQuery = source as ObjectQuery<TSource>;
return objectQuery == null ? source : objectQuery.Include(path);
}
它确实添加了 Include() 方法,但是我现在收到此错误:
LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[PostHope.Core.DomainObjects.SiteAnnouncement] Include[SiteAnnouncement](System.Linq.IQueryable`1[PostHope.Core.DomainObjects.SiteAnnouncement], System.String)' method, and this method cannot be translated into a store expression.
【问题讨论】:
-
你是如何调用 Include 方法的?
-
来自扩展方法(来自 Julie Lerman 的博客thedatafarm.com/blog/data-access/…)也在她的新书中。仍然没有找到解决办法。这个帖子也是同样的问题:stackoverflow.com/questions/2655886/…