EF6 Linq查询不返回某些参数值的结果,但SQL语句会返回

时间:2015-08-26 13:29:45

标签: entity-framework tsql

问题摘要: 基于屏幕上的选择显示记录的Web应用程序。这些记录作为EF查询的结果返回,该查询采用两个参数。对于一个特定的选择,没有返回结果,但如果我自己编写一个sql语句,我可以获取数据。但是,EF查询会按预期返回其他输入值的结果。

比较EF查询和SQL查询的奇怪场景。我的一些数据我正在运行一些检查,寻找孤立的记录,并发现似乎是一些。使用访问相同数据库的应用程序,没有通过EF查询返回的记录运行(这是我所期望的)。然而,在我自己运行一些测试查询之后,当我发现一些应该应该返回给应用程序的数据时。我用profiler捕获了那个EF查询,并写了我认为是SQL中的等价语句来与之比较。

我更新了我的模型,重建了项目等,但其核心问题似乎是数据依赖,因为EF查询返回结果

问题在于,对于选定的数据参数(输入),我在自己编写的SQL查询中得到结果,但是从EF生成的SQL查询中得不到任何结果 - 这导致使用sp_executesql执行动态sql语句。 / p>

EF SQL(返回空

exec sp_executesql N'SELECT 
    [Project1].[GoalId] AS [GoalId], 
    [Project1].[Year] AS [Year], 
    [Project1].[Name2] AS [Name], 
    [Project1].[BusinessLineId] AS [BusinessLineId], 
    [Project1].[Name1] AS [Name1], 
    [Project1].[CommodityId] AS [CommodityId], 
    [Project1].[DivisionId] AS [DivisionId], 
    [Project1].[Name3] AS [Name2], 
    [Project1].[MetricTypeId] AS [MetricTypeId], 
    [Project1].[Name] AS [Name3], 
    [Project1].[TargetAvg] AS [TargetAvg], 
    [Project1].[TargetTotal] AS [TargetTotal], 
    [Project1].[Projected] AS [Projected]
    FROM ( SELECT 
        [Extent1].[GoalId] AS [GoalId], 
        [Extent1].[Year] AS [Year], 
        [Extent1].[TargetTotal] AS [TargetTotal], 
        [Extent1].[TargetAvg] AS [TargetAvg], 
        [Extent1].[Projected] AS [Projected], 
        [Extent2].[Name] AS [Name], 
        [Extent4].[CommodityId] AS [CommodityId], 
        [Extent4].[Name] AS [Name1], 
        [Extent5].[BusinessLineId] AS [BusinessLineId], 
        [Extent5].[Name] AS [Name2], 
        [Extent7].[MetricTypeId] AS [MetricTypeId], 
        [Extent7].[Name] AS [Name3], 
        [Extent9].[DivisionId] AS [DivisionId]
        FROM         [dbo].[Goal] AS [Extent1]
        INNER JOIN [dbo].[GoalSpread] AS [Extent2] ON [Extent1].[GoalSpreadKey] = [Extent2].[GoalSpreadId]
        INNER JOIN [dbo].[BusinessLineCommodity] AS [Extent3] ON [Extent1].[BusinessLineCommodityKey] = [Extent3].[Id]
        INNER JOIN [dbo].[Commodity] AS [Extent4] ON [Extent3].[CommodityKey] = [Extent4].[CommodityId]
        INNER JOIN [dbo].[BusinessLine] AS [Extent5] ON [Extent3].[BusinessLineKey] = [Extent5].[BusinessLineId]
        INNER JOIN [dbo].[BusinessLineMetricType] AS [Extent6] ON [Extent5].[BusinessLineId] = [Extent6].[BusinessLineKey]
        INNER JOIN [dbo].[MetricType] AS [Extent7] ON [Extent6].[MetricTypeKey] = [Extent7].[MetricTypeId]
        INNER JOIN [dbo].[DivisionBusinessLine] AS [Extent8] ON [Extent5].[BusinessLineId] = [Extent8].[BusinessLineKey]
        INNER JOIN [dbo].[Division] AS [Extent9] ON [Extent8].[DivisionKey] = [Extent9].[DivisionId]
        WHERE (([Extent9].[Code] = @p__linq__0) ) AND ([Extent1].[Year] = @p__linq__1)
    )  AS [Project1]
    ORDER BY [Project1].[BusinessLineId] ASC',N'@p__linq__0 varchar(8000),@p__linq__1 int',@p__linq__0='De664f9ec-6f7a-4434-bdfc-b7832a3c9111',@p__linq__1=2015

我要测试的SQL语句:(这会返回结果

    --test
SELECT * 
FROM dbo.Goal g
JOIN dbo.GoalSpread gs  ON g.GoalSpreadKey = gs.GoalSpreadId
JOIN dbo.BusinessLineCommodity blc ON g.BusinessLineCommodityKey = blc.Id
JOIN dbo.Commodity c ON blc.CommodityKey = c.CommodityId
JOIN dbo.BusinessLine bl ON blc.BusinessLineKey = bl.BusinessLineId
JOIN dbo.BusinessLineMetricType blm ON bl.BusinessLineId = blm.BusinessLineKey
JOIN dbo.MetricType mt ON blm.MetricTypeKey = mt.MetricTypeId
JOIN dbo.DivisionBusinessLine dbl ON bl.BusinessLineId = dbl.BusinessLineKey
JOIN dbo.Division d ON g.DivisionKey = d.DivisionId
WHERE   d.Code = 'De664f9ec-6f7a-4434-bdfc-b7832a3c9111' AND g.Year = 2015

1 个答案:

答案 0 :(得分:2)

查看两个查询并查找它如何加入。 最好的方法是使用记事本替换所有[Extent1]与EF#查询,对EF中的所有别名执行,并使用diff工具来比较和发现差异。

以下一个例子:

SSMS:

FROM dbo.Goal g
(...)
JOIN dbo.Division d ON g.DivisionKey = d.DivisionId

EF:

FROM [dbo].[Goal] AS [Extent1]
(...)
INNER JOIN [dbo].[Division] AS [Extent9] ON [Extent8].[DivisionKey] = [Extent9].[DivisionId]

EF查询替换为您的格式,在SSMS中执行此操作,应该不返回任何内容。

SELECT * 
FROM dbo.Goal g
JOIN dbo.GoalSpread gs  ON g.GoalSpreadKey = gs.GoalSpreadId
JOIN dbo.BusinessLineCommodity blc ON g.BusinessLineCommodityKey = blc.Id
JOIN dbo.Commodity c ON blc.CommodityKey = c.CommodityId
JOIN dbo.BusinessLine bl ON blc.BusinessLineKey = bl.BusinessLineId
JOIN dbo.BusinessLineMetricType blm ON bl.BusinessLineId = blm.BusinessLineKey
JOIN dbo.MetricType mt ON blm.MetricTypeKey = mt.MetricTypeId
JOIN dbo.DivisionBusinessLine dbl ON bl.BusinessLineId = dbl.BusinessLineKey
/*JOIN dbo.Division d ON g.DivisionKey = d.DivisionId*/
JOIN dbo.Division d ON dbl.DivisionKey = d.DivisionId
WHERE   d.Code = 'De664f9ec-6f7a-4434-bdfc-b7832a3c9111' AND g.Year = 2015