LINQ to Entities查询不投影子集合

时间:2013-03-21 20:26:19

标签: c# entity-framework

什么会导致此查询。仅开始投影c而不填充其子集合ParcelValues

var result = (from c in _context.Parcels.Include("ParcelValues")
where c.STRAP == strap
select c);

我正在运行我的应用程序,上面会返回result类型为IQueryable<Parcel>的对象,其中属性Request.ParcelValues填充了正确的记录。现在,出于某种原因,无论我如何尝试使用include,都只会投影Parcel字段。

生成SQL

exec sp_executesql N'SELECT 
[Project2].[LAND_AREA] AS [LAND_AREA], 
[Project2].[STRAP] AS [STRAP], 
[Project2].[SITUS] AS [SITUS], 
[Project2].[ZIP_CODE] AS [ZIP_CODE], 
[Project2].[MAILING_ADDRESS] AS [MAILING_ADDRESS], 
[Project2].[PROPERTY_USE] AS [PROPERTY_USE], 
[Project2].[SUBDIVISION] AS [SUBDIVISION], 
[Project2].[DSCR] AS [DSCR], 
[Project2].[INCORPORATION] AS [INCORPORATION], 
[Project2].[SEC_TWP_RGE] AS [SEC_TWP_RGE], 
[Project2].[CENSUS] AS [CENSUS], 
[Project2].[WATERFRONT] AS [WATERFRONT], 
[Project2].[C1] AS [C1], 
[Project2].[ID] AS [ID], 
[Project2].[STRAP1] AS [STRAP1], 
[Project2].[YEAR] AS [YEAR], 
[Project2].[LAND] AS [LAND], 
[Project2].[IMPROVEMENT] AS [IMPROVEMENT], 
[Project2].[JUST] AS [JUST], 
[Project2].[ASSESSED] AS [ASSESSED], 
[Project2].[TAXABLE] AS [TAXABLE]
FROM ( SELECT 
    [Limit1].[STRAP] AS [STRAP], 
    [Limit1].[SITUS] AS [SITUS], 
    [Limit1].[ZIP_CODE] AS [ZIP_CODE], 
    [Limit1].[MAILING_ADDRESS] AS [MAILING_ADDRESS], 
    [Limit1].[PROPERTY_USE] AS [PROPERTY_USE], 
    [Limit1].[SUBDIVISION] AS [SUBDIVISION], 
    [Limit1].[DSCR] AS [DSCR], 
    [Limit1].[LAND_AREA] AS [LAND_AREA], 
    [Limit1].[INCORPORATION] AS [INCORPORATION], 
    [Limit1].[SEC_TWP_RGE] AS [SEC_TWP_RGE], 
    [Limit1].[CENSUS] AS [CENSUS], 
    [Limit1].[WATERFRONT] AS [WATERFRONT], 
    [Extent2].[ID] AS [ID], 
    [Extent2].[STRAP] AS [STRAP1], 
    [Extent2].[YEAR] AS [YEAR], 
    [Extent2].[LAND] AS [LAND], 
    [Extent2].[IMPROVEMENT] AS [IMPROVEMENT], 
    [Extent2].[JUST] AS [JUST], 
    [Extent2].[ASSESSED] AS [ASSESSED], 
    [Extent2].[TAXABLE] AS [TAXABLE], 
    CASE WHEN ([Extent2].[ID] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1]
    FROM   (SELECT TOP (1) 
        [Extent1].[STRAP] AS [STRAP], 
        [Extent1].[SITUS] AS [SITUS], 
        [Extent1].[ZIP_CODE] AS [ZIP_CODE], 
        [Extent1].[MAILING_ADDRESS] AS [MAILING_ADDRESS], 
        [Extent1].[PROPERTY_USE] AS [PROPERTY_USE], 
        [Extent1].[SUBDIVISION] AS [SUBDIVISION], 
        [Extent1].[DSCR] AS [DSCR], 
        [Extent1].[LAND_AREA] AS [LAND_AREA], 
        [Extent1].[INCORPORATION] AS [INCORPORATION], 
        [Extent1].[SEC_TWP_RGE] AS [SEC_TWP_RGE], 
        [Extent1].[CENSUS] AS [CENSUS], 
        [Extent1].[WATERFRONT] AS [WATERFRONT]
        FROM [dbo].[PARCELS] AS [Extent1]
        WHERE [Extent1].[STRAP] = @p__linq__0 ) AS [Limit1]
    LEFT OUTER JOIN [dbo].[PARCEL_VALUES] AS [Extent2] ON [Limit1].[STRAP] = [Extent2].[STRAP]
)  AS [Project2]
ORDER BY [Project2].[STRAP] ASC, [Project2].[C1] ASC',N'@p__linq__0 nvarchar(4000)',@p__linq__0=N'0001020003'

1 个答案:

答案 0 :(得分:0)

了解数据

因此,它最终导致我的整个问题是由实体框架的已知设计特征/错误(取决于您如何看待它)和我的数据引起的。我用nchar(25)将表中的键导入到带有varchar(16)的表中。大多数表都是在填充目标之前修剪键。我忘了修剪那些似乎永远不会加载的表上的键,除非单独查询。所以,事实证明EF不能延迟加载相关的表,其中一些在键中具有尾随空格(空格也必须匹配)。

一旦我更新了我的脚本以加载我的所有目标sans空白。问题已解决。