实体框架4:预先加载(包括)使用自动跟踪​​实体过滤器 [英] Entity Framework 4: Eager Loading (Include) with filters using Self Tracking Entities

查看:117
本文介绍了实体框架4:预先加载(包括)使用自动跟踪​​实体过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,我已经创建了使用RTM模板自跟踪实体的解决方案。我已经分手了实体和上下文2个项目之间,这样我可以重复使用的类型定义,因为我打算通过WCF运行客户机/服务器。

I have a solution where I have created self tracking entities using the RTM templates. I have split the entities and context between 2 projects so that I can reuse the type definitions as I plan to run client/server via WCF.

我的一个服务方法需要返回产品物体ProductSku的子对象的图形,这些反过来又ProductPrice的子对象。选择的标准将在产品对象的名称属性,而ProductPriceObject的FinancialPeriodID属性。现在,我不包括在搜索中的名字,但我有带回图问题。

One of my service methods is required to return a graph of "Product" objects with child objects of "ProductSku" and these in turn have child objects of "ProductPrice". The selection criteria will be on the "Name" property of the "Product" object, and the "FinancialPeriodID" property of the "ProductPriceObject". For now, I am not including the name in the search, but I am having problems bringing back the graph.

如果我只是执行下面的查询(请注意,这个语法是从LinqPad而不是实际的应用code)...

If I simply perform the following query (note, this syntax is taken from LinqPad rather than the actual application code)...

from product in Products.Include("Skus.PriceHistory")
select product

...那么我能够检索,当然了,我需要的物品全部对象图,在这一点上没有任何过滤器。

... then I am able to retrieve the full object graph for the items that I require, of course at this point there is no filter.

如果不是,我介绍的过滤器如下...

If instead, I introduce the filter as follows...

from product in Products.Include("Skus.PriceHistory")
join sku in ProductSkus on product.ID equals sku.ProductID
join price in ProductPrices on sku.ID equals price.ProductSkuID
where price.FinancialPeriodID == 244
select product

...我所期待得到的回复是产品的对象,孩子ProductSku对象(这是在产品的单品集合)和他们的ProductPrice对象(这是在ProductSku)的PriceHistory收集 - 但我只拿回了产品的对象,在单品集合为空

... what I am expecting to get back is the "Product" objects, the child "ProductSku" objects (which are in the "Skus" collection of the "Product") and their "ProductPrice" objects (which are in the "PriceHistory" collection of the "ProductSku") - but I only get back the "Product" objects, the "Skus" collection is empty.

我也曾尝试编码查询作为...

I have also tried coding the query as ...

from product in Products.Include("Skus.PriceHistory")
from sku in product.Skus
from price in sku.PriceHistory
where price.FinancialPeriodID == 244
select product

...但是这没有区别无论是。

... but this makes no difference either.

显然,我必须做一些错误的。任何人都可以摆脱任何光线上的东西是什么,因为我已经在这几个小时,现在兜兜转转!

Clearly, I must be doing something wrong. Can anybody shed any light on what that something is as I have been at this for some hours now going around in circles!

推荐答案

编辑:

什么:

from product in Products.Include("Skus.PriceHistory")
where product.Skus.Any(s => s.PriceHistory.Any(p => p.FinancialPeriodID == 244))
select product

包含已完成所有必要的工作,以填补导航属性,以便更多的连接在不需要条件。什么是更重要的是任何人工参与或投影将改变查询的形状和包含将不会被使用。

Include already performs all necessary tasks to fill navigation properties so additional joins for where condition are not needed. What is even more important any manual join or projection will change the shape of the query and Include will not be used.

另外要注意,where条件过滤器只产品。它不会过滤由包括加载数据 - 你会得到所有的产品具有价格历史财务期间ID 244至少有一个SKU,但这些产品将加载所有SKU及价格的历史。 EF目前不支持过滤功能包括。如果您需要过滤的关系,以及你必须执行不同的查询来获得它们。

Also beware that where condition filters only products. It will not filter data loaded by Include - you will get all products with at least one sku having price history with financial period id 244 but those products will have all skus and price histories loaded. EF currently does not support filtering on include. If you need filtered relations as well you have to execute separate queries to get them.

这篇关于实体框架4:预先加载(包括)使用自动跟踪​​实体过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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