EF返回不正确的行

时间:2014-07-10 09:14:31

标签: c# .net entity-framework

我有一个简单的查询:

IEnumerable<AmountChangeDocumentItemExtended> GetItemsByHeaderId(Guid headerId){
    var results = (from item in _context.AmountChangeDocumentItems
                   where item.HeaderId == headerId
                   select new AmountChangeDocumentItemExtended
                   {.....}).ToArray();
    return results;
}

它返回0行,但是当我使用普通查询(ExecuteStoreQuery&lt;&gt;())获取行时,我会收到实际数据。

_context.ExecuteStoreQuery<AmountChangeDocumentItem>(@"SELECT *
FROM
    dbo.AmountChangeDocumentItems i
WHERE i.HeaderId = '201dae45-64a6-4b49-b801-c8fb20165ee5'");

我不明白,为什么第一种方法不起作用?

  1. 对于任何其他guid,两种方法都返回正确的行...
  2. 在第一种情况下,我在SQL Server分析器中看不到任何收入查询......
  3. UPD : 跟踪字符串:

    SELECT 
    [Extent1].[ProductCode] AS [ProductCode], 
    [Extent1].[Id] AS [Id], 
    [Extent1].[HeaderId] AS [HeaderId], 
    [Extent1].[ProductVersion] AS [ProductVersion], 
    [Extent1].[Amount] AS [Amount], 
    [Extent1].[PriceCalculatorVersion] AS [PriceCalculatorVersion], 
    [Extent1].[FirstSupplierPrice] AS [FirstSupplierPrice], 
    [Extent1].[NonTaxablePart] AS [NonTaxablePart], 
    [Extent1].[SupplierTaxRate] AS [SupplierTaxRate], 
    [Extent1].[SupplierInterestRate] AS [SupplierInterestRate], 
    [Extent1].[RetailTaxRate] AS [RetailTaxRate], 
    [Extent1].[RetailInterestRate] AS [RetailInterestRate], 
    [Extent1].[RetailPrice] AS [RetailPrice], 
    [Extent2].[AgentCode] AS [AgentCode], 
    [Extent3].[Name] AS [Name], 
    [Extent1].[ItemOrder] AS [ItemOrder], 
    [Extent1].[Discount] AS [Discount], 
    [Extent1].[ExpectedRetailFinalPriceTotal] AS [ExpectedRetailFinalPriceTotal], 
    [Extent4].[BarCodeString] AS [BarCodeString], 
    [Extent4].[Name] AS [Name1], 
    [Extent4].[CountryOfOrigin] AS [CountryOfOrigin], 
    [Extent4].[UnitOfMeasureCode] AS [UnitOfMeasureCode], 
    [Extent5].[ShortName] AS [ShortName], 
    [Extent6].[IsFractional] AS [IsFractional], 
    [Extent1].[CheckId] AS [CheckId], 
    CASE WHEN ([Extent1].[CheckId] IS NOT NULL) THEN [Extent7].[Number] END AS [C1], 
    CASE WHEN ([Extent1].[CheckId] IS NOT NULL) THEN [Extent7].[Sum] END AS [C2],
    [Extent1].[AlternateBarCode] AS [AlternateBarCode], 
    [Extent1].[AlternateBarCodeCoeff] AS [AlternateBarCodeCoeff], 
    [Extent1].[PalmInitialAmount] AS [PalmInitialAmount], 
    [Extent1].[PalmInitialRetailPrice] AS [PalmInitialRetailPrice], 
    [Extent1].[PalmOutAmount] AS [PalmOutAmount], 
    [Extent1].[PalmOutRetailPrice] AS [PalmOutRetailPrice]
    FROM       [dbo].[AmountChangeDocumentItems] AS [Extent1]
    INNER JOIN [dbo].[CurrentSuppliers] AS [Extent2] ON ([Extent1].[ProductCode] = [Extent2].[ProductCode]) AND ([Extent1].[ProductVersion] = [Extent2].[ProductVersion])
    LEFT OUTER JOIN [dbo].[Agents] AS [Extent3] ON [Extent2].[AgentCode] = [Extent3].[Code]
    LEFT OUTER JOIN [dbo].[Products] AS [Extent4] ON ([Extent1].[ProductCode] = [Extent4].[InternalCode]) AND ([Extent1].[ProductVersion] = [Extent4].[Version])
    LEFT OUTER JOIN [dbo].[UnitsOfMeasure] AS [Extent5] ON [Extent4].[UnitOfMeasureCode] = [Extent5].[Code]
    LEFT OUTER JOIN [dbo].[UnitsOfMeasure] AS [Extent6] ON [Extent4].[UnitOfMeasureCode] = [Extent6].[Code]
    LEFT OUTER JOIN [dbo].[CheckItems] AS [Extent7] ON [Extent1].[CheckId] = [Extent7].[Id]
    WHERE [Extent1].[HeaderId] = @p__linq__0
    

1 个答案:

答案 0 :(得分:0)

问题出现在我的edmx模型中:我的关联乘数不正确(1 - 1,1 - 0..1),所以EF生成INNER JOIN insfts JOIN。