实体框架6和LINQ - 重复记录

时间:2014-03-08 04:47:10

标签: c# .net linq entity-framework

我有一个标准的实体框架实现直接查询表,根本没有涉及任何视图,使用以下代码:

  

var r = cxt.HistoricalQualityComponents.Where(f => f.ProducerID == activeProducer);

一切正常,我期待24条记录,我得到24条记录。除了当我查看r的内容时,我看到第一个返回的记录,重复24次。我查看生成的SQL并直接对数据库运行它,我得到24个唯一记录。我通过观察activeProducer更改时的内容,甚至将排序作为LINQ查询的一部分来实现。我对如何解决这个问题感到很茫然。这在我的数据库中有两个表,但没有其他表。

我试图删除我的.edmx实体文件,我重命名了表(和生成的实体),并创建了一个完整的.aspx文件来处理实体。当然,我重写了这样的查询:

  

var r = from c in cxt.HistoricalQualityComponents其中h.ProducerId == activeProducer select h;

没有什么区别。

这可能完全无关紧要,但我在(非常懒惰地)在两个结果集上尝试以下后都注意到了这种行为:

  

r.Sort(delegate(HistoricalQualityComponents c1,HistoricalQualityComponents c2){return Convert.ToDateTime(c2.Pickup_Date).CompareTo(Convert.ToDateTime(c1.Pickup_Date));});

此时,我只想回到一个干净的状态,这样我就可以实现一个模型类,将数据存储到一个正确类型的对象中,我可以更适当地对它进行排序,但是我不知所措。去哪儿。

更新:生成的SQL如下:

SELECT 
    [Extent1].[RcdID] AS [RcdID], 
    [Extent1].[FieldRepID] AS [FieldRepID], 
    [Extent1].[ProducerID] AS [ProducerID], 
    [Extent1].[MonthYear_PK] AS [MonthYear_PK], 
    [Extent1].[Weight] AS [Weight], 
    [Extent1].[Fat] AS [Fat], 
    [Extent1].[Protein] AS [Protein], 
    [Extent1].[Lactose] AS [Lactose], 
    [Extent1].[SNF] AS [SNF], 
    [Extent1].[StateLeuc] AS [StateLeuc], 
    [Extent1].[Leuc] AS [Leuc], 
    [Extent1].[StateRaw] AS [StateRaw], 
    [Extent1].[Raw] AS [Raw], 
    [Extent1].[RawBracket] AS [RawBracket], 
    [Extent1].[PI] AS [PI], 
    [Extent1].[PIBracket] AS [PIBracket], 
    [Extent1].[lpc_avg] AS [lpc_avg], 
    [Extent1].[Water] AS [Water], 
    [Extent1].[AB] AS [AB], 
    [Extent1].[SED] AS [SED], 
    [Extent1].[mun] AS [mun], 
    [Extent1].[LStd] AS [LStd], 
    [Extent1].[RStd] AS [RStd], 
    [Extent1].[PStd] AS [PStd], 
    [Extent1].[SStd] AS [SStd], 
    [Extent1].[QualPremRate] AS [QualPremRate], 
    [Extent1].[QualPremAmt] AS [QualPremAmt], 
    [Extent1].[FYTDQualScore] AS [FYTDQualScore], 
    [Extent1].[FYTDQualPrem] AS [FYTDQualPrem], 
    [Extent1].[FYTDAvgQualPremRate] AS [FYTDAvgQualPremRate], 
    [Extent1].[OtherSolids] AS [OtherSolids], 
    [Extent1].[AshFactor] AS [AshFactor], 
    [Extent1].[ash_wtd_avg] AS [ash_wtd_avg], 
    [Extent1].[fat_wtd_avg] AS [fat_wtd_avg], 
    [Extent1].[leuc_wtd_avg] AS [leuc_wtd_avg], 
    [Extent1].[protein_wtd_avg] AS [protein_wtd_avg], 
    [Extent1].[os_wtd_avg] AS [os_wtd_avg], 
    [Extent1].[snf_wtd_avg] AS [snf_wtd_avg], 
    [Extent1].[iodine_wtd_avg] AS [iodine_wtd_avg]
FROM (SELECT 
    [HistoricalQualityComponents].[RcdID] AS [RcdID], 
    [HistoricalQualityComponents].[FieldRepID] AS [FieldRepID], 
    [HistoricalQualityComponents].[ProducerID] AS [ProducerID], 
    [HistoricalQualityComponents].[MonthYear_PK] AS [MonthYear_PK], 
    [HistoricalQualityComponents].[Weight] AS [Weight], 
    [HistoricalQualityComponents].[Fat] AS [Fat], 
    [HistoricalQualityComponents].[Protein] AS [Protein], 
    [HistoricalQualityComponents].[Lactose] AS [Lactose], 
    [HistoricalQualityComponents].[SNF] AS [SNF], 
    [HistoricalQualityComponents].[StateLeuc] AS [StateLeuc], 
    [HistoricalQualityComponents].[Leuc] AS [Leuc], 
    [HistoricalQualityComponents].[StateRaw] AS [StateRaw], 
    [HistoricalQualityComponents].[Raw] AS [Raw], 
    [HistoricalQualityComponents].[RawBracket] AS [RawBracket], 
    [HistoricalQualityComponents].[PI] AS [PI], 
    [HistoricalQualityComponents].[PIBracket] AS [PIBracket], 
    [HistoricalQualityComponents].[lpc_avg] AS [lpc_avg], 
    [HistoricalQualityComponents].[Water] AS [Water], 
    [HistoricalQualityComponents].[AB] AS [AB], 
    [HistoricalQualityComponents].[SED] AS [SED], 
    [HistoricalQualityComponents].[mun] AS [mun], 
    [HistoricalQualityComponents].[LStd] AS [LStd], 
    [HistoricalQualityComponents].[RStd] AS [RStd], 
    [HistoricalQualityComponents].[PStd] AS [PStd], 
    [HistoricalQualityComponents].[SStd] AS [SStd], 
    [HistoricalQualityComponents].[QualPremRate] AS [QualPremRate], 
    [HistoricalQualityComponents].[QualPremAmt] AS [QualPremAmt], 
    [HistoricalQualityComponents].[FYTDQualScore] AS [FYTDQualScore], 
    [HistoricalQualityComponents].[FYTDQualPrem] AS [FYTDQualPrem], 
    [HistoricalQualityComponents].[FYTDAvgQualPremRate] AS [FYTDAvgQualPremRate], 
    [HistoricalQualityComponents].[OtherSolids] AS [OtherSolids], 
    [HistoricalQualityComponents].[AshFactor] AS [AshFactor], 
    [HistoricalQualityComponents].[ash_wtd_avg] AS [ash_wtd_avg], 
    [HistoricalQualityComponents].[fat_wtd_avg] AS [fat_wtd_avg], 
    [HistoricalQualityComponents].[leuc_wtd_avg] AS [leuc_wtd_avg], 
    [HistoricalQualityComponents].[protein_wtd_avg] AS [protein_wtd_avg], 
    [HistoricalQualityComponents].[os_wtd_avg] AS [os_wtd_avg], 
    [HistoricalQualityComponents].[snf_wtd_avg] AS [snf_wtd_avg], 
    [HistoricalQualityComponents].[iodine_wtd_avg] AS [iodine_wtd_avg]
FROM 
    [dbo].[HistoricalQualityComponents] AS [HistoricalQualityComponents]) AS [Extent1]
WHERE 
    [Extent1].[ProducerID] = @p__linq__0

1 个答案:

答案 0 :(得分:1)

如果实体模型中的键与数据库表中的键不匹配,则可能发生这种情况。

例如,如果数据库中的键是由(RcdID, FieldRepID)组成的组合键,但在模型实体中,只有RcdID被定义为键,并且您运行的查询将返回值为{{的三行1}},(1, 1)(1, 2)您可能会获得值为(1, 3)三次的实体列表。

原因是EF只“看到”该对的第一部分(1, 1)为关键。此键值返回三次。因为EF每个键仅将一个对象附加到上下文,所以第一行实现为具有键1的实体,但是对于第二行和第三行,没有实现新实体。而是将第一行的已附加实体第二次和第三次添加到结果列表中。

长话短说:检查实体模型中定义的键是否与数据库模式中的键匹配。