2010-08-07 88 views
2

我首先使用EF4 CPT4代码,并且我已经设置了我的ObjectContext以返回IObjectSet,因此我可以模拟和测试我的回购。但是,我现在注意到,我无法使用.Include()方法进行加载,就像我可以使用ObjectSet一样。实体框架4 IObjectSet包括

有没有办法让这个工作?

编辑: 我添加了这个扩展方法:

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. 
+0

你是怎么调用Include方法的? – Jonathan 2010-10-04 23:26:35

+0

从扩展方法(来自Julie Lerman的博客http://thedatafarm.com/blog/data-access/agile-entity-framework-4-repository-part-5-iobjectset/)也在她的新书中。还没有找到解决方案。这篇文章是相同的问题:http://stackoverflow.com/questions/2655886/iobjectset-include-extension-method-errors-with-compiledquery – Paul 2010-10-05 17:22:02

回答

1

由EF团队回应:

这是CTP4的一个已知问题,Include是ObjectSet上的一个实例方法,但是当您的set被键入为IObjectSet时,您实际上是在IQ上使用扩展方法包含在CTP4中的ueryable。这种扩展方法不适用于编译查询,但我们将在下一个版本中尝试并支持这一点。

〜Rowan