实体框架4.1 code首先 - 包括正在使用时,忽略LinqKit predicateBuilder [英] Entity Framework 4.1 Code First - Include is being ignored when using LinqKit PredicateBuilder

查看:240
本文介绍了实体框架4.1 code首先 - 包括正在使用时,忽略LinqKit predicateBuilder的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C首先实体框架4.1 $ C $,并使用predicateBuilder这样我就可以建立predicate EX $ P $多个规范类(使用规范模式)pssions我。我能够正确地构建predicate并将其应用到一个DbSet,而我得到的数据是我所期望的。但是,不管我怎么努力,总是懒加载。这就是我如何建立predicate并将其应用于一个简单的例子。

I am using Entity Framework 4.1 Code First and am also using the PredicateBuilder so that I can build predicate expressions across multiple Specification classes (using the Specification pattern). I am able to properly build a predicate and apply it to a DbSet, and the data I get is what I expect. However, no matter what I try, it's always lazy loading. This is a simple example of how I'm building the predicate and applying it.

IQueryable<HairColor> hairColorQuery = Context.Set<HairColor>().AsExpandable();

Expression<Func<Parent, bool>> parentPredicate = PredicateBuilder.And(PredicateBuilder.True<Parent>(), p => p.NameLast.StartsWith("V")).Expand();

Expression<Func<HairColor, bool>> hairColorPredicate = PredicateBuilder.And(PredicateBuilder.True<HairColor>(), h => h.Parents.AsQueryable().Any(parentPredicate));

HairColor[] hairColors = hairColorQuery.Where(hairColorPredicate).Include(h => h.Parents).ToArray();

就像我上面说的,我得到了我想要的数据背后,却忽略了包括

Like I said above, I'm getting the data back that I want, but it ignores the Include.

有没有人有什么想法?

推荐答案

使用LinqKits时predicate建设者跳跃在后期这个喜,但使用扩展有同样的问题包括法。如在previous答案提到的问题是,铸造LinqKits ExpandableQuery到的ObjectQuery(所要求的包括扩展名)的结果在空。

Hi jumping in late on this, but had the same issue with using an extension Include method when using LinqKits predicate builder. The problem as referred to in the previous answer is that casting LinqKits ExpandableQuery to ObjectQuery (as required by the Include extension) results in null.

但是发现这个链接<一个href="http://petemontgomery.word$p$pss.com/2011/02/10/a-universal-$p$pdicatebuilder/">http://petemontgomery.word$p$pss.com/2011/02/10/a-universal-$p$pdicatebuilder/其是predicate助洗剂不使用AsExpandable执行搜索,因此包含方法可以在其上使用。尽管上述解决方案也为我工作这两样predicate建设者让我保持我的code清洁/更稳定的

However found this link http://petemontgomery.wordpress.com/2011/02/10/a-universal-predicatebuilder/ which is a Predicate builder which doesn't use AsExpandable to perform the search and hence the Include method can be used on it. Although the solution above also worked for me this different predicate builder allowed me to keep my code cleaner/more consistent

这篇关于实体框架4.1 code首先 - 包括正在使用时,忽略LinqKit predicateBuilder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆