简体   繁体   English

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

[英]Entity Framework 6 and LINQ - Duplicated Records

I have a standard entity framework implementation querying directly against a table, no views involved at all, with the following code: 我有一个标准的实体框架实现,直接使用表查询,根本不涉及任何视图,并使用以下代码:

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

Everything works properly, I would expect 24 records and I get 24 records. 一切正常,我希望有24条记录,而我得到24条记录。 Except that when I look at the contents of r, I see the first returned record, repeated 24 times. 除了查看r的内容外,我看到了第一个返回的记录,重复了24次。 I look at the generated SQL and run it directly against the database and I get 24 unique records. 我查看生成的SQL,然后直接对数据库运行它,并获得24条唯一记录。 I conclude this by observing the contents while activeProducer changes, or even applying a sort as part of the LINQ query. 我通过在activeProducer更改时观察内容,甚至在LINQ查询中应用排序来得出结论。 I'm at a loss at how to fix this. 我不知如何解决此问题。 This occurs with two tables in my database, but no others. 我的数据库中有两个表,但没有其他表。

I've tried to delete my .edmx entity file, I renamed the table (and generated entity), and created an entirely .aspx file to process the entity. 我试图删除.edmx实体文件,重命名了表(并生成了实体),并创建了一个完整的.aspx文件来处理该实体。 Of course, I reworked the query as such: 当然,我对查询做了如下修改:

var r = from h in cxt.HistoricalQualityComponents where h.ProducerId == activeProducer select h; var r =从cxt.HistoricalQualityComponents中的h开始,其中h.ProducerId == activeProducer选择h;

Didn't make a difference. 没什么不同。

It may be entirely irrelevant, but I noticed this behavior after (very lazily) trying the following on both result sets: 这可能是完全不相关的,但是在(非常懒惰)在两个结果集上尝试以下操作后,我注意到了此行为:

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

At this point, I'd simply like to get back to a clean state so that I can implement a model class that will store the data into a properly typed object and I can sort it more appropriately but am at a loss on where to go next. 在这一点上,我只是想回到一个干净的状态,以便我可以实现一个模型类,该模型类将数据存储到正确类型的对象中,并且我可以对其进行更适当的排序,但是迷失了去向下一个。

UPDATE: The generated SQL follows: 更新:生成的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

It could happen if the keys in your entity model don't match the keys in the database table. 如果您的实体模型中的键与数据库表中的键不匹配,则可能会发生这种情况。

For example, if the key in the database is composite consisting of (RcdID, FieldRepID) but in your model entity only RcdID is defined as key and you run a query that would return three rows with values (1, 1) , (1, 2) and (1, 3) you would probably get a list of the entity with the values (1, 1) three times. 例如,如果数据库中的键是由(RcdID, FieldRepID)组成的复合键,但是在模型实体中,只有RcdID被定义为键,并且您运行的查询将返回三行,其值分别为(1, 1)(1, 2)(1, 3)您可能会获得三次具有值(1, 1)的实体列表。

The reason is that EF only "sees" the first part 1 of the pair as key. 原因是EF仅“看到”该对的第一部分1作为密钥。 This key value is returned three times. 该键值被返回三次。 Because EF attaches only one object per key to the context the first row is materialized to an entity with key 1 , but for the second and third row no new entity is materialized. 因为EF每个键仅将一个对象附加到上下文,所以第一行被实现为具有键1的实体,但是对于第二行和第三行,则没有新的实体被实现。 Instead the already attached entity of the first row is added a second and third time to the result list. 而是将第一行的已附加实体第二次和第三次添加到结果列表中。

Long story short: Check if the keys defined in the entity model match the keys in the database schema. 简而言之:检查实体模型中定义的键是否与数据库模式中的键匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在实体框架中插入重复的记录 - How to insert duplicated records in Entity Framework 实体框架核心正在尝试在调用SaveChanges()时插入重复的记录 - Entity Framework Core is trying to insert duplicated records when SaveChanges() is called 使用单个LINQ查询使用Entity Framework删除多个记录 - Deleting multiple records with Entity Framework using a single LINQ query 代码第一个实体框架Linq语句返回丢失的记录 - Code First Entity Framework Linq Statement Returning Missing Records 实体框架linq查询以查找带有土耳其字符的记录 - Entity Framework linq query to find records with turkish characters 实体框架 LINQ 等效于 TSQL 以包括子记录计数 - Entity Framework LINQ equivalent of TSQL to include count of child records 使用Linq通过实体框架关系将父记录短名单化 - Use Linq to shortlist parent records by using Entity Framework relations 动态LINQ-实体框架6-动态选择的更新记录 - Dynamic LINQ - Entity Framework 6 - Update Records for Dynamic Select 如何使用 Entity Framework Core 的 Linq 更新表中的多个记录 - How to update multiple records in a table Using the Linq for Entity Framework Core 实体框架重复列问题 - Entity Framework duplicated column issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM