Bug #62801 Strange behaviour with Include in LINQ-to-Entities
Submitted: 18 Oct 2011 20:27 Modified: 11 Apr 2012 17:50
Reporter: James Gurung Email Updates:
Status: Closed Impact on me:
None 
Category:Connector / NET Severity:S3 (Non-critical)
Version:6.4.4.0 OS:Any
Assigned to: Gabriela Martinez Sanchez CPU Architecture:Any
Tags: .net, connector, EF4, entity

[18 Oct 2011 20:27] James Gurung
Description:
In LINQ-to-Entities, when you 'Include' a child entity (1-n), and its entities (n-n) the wrong results are returned.

For example: db.Authors.Include("Books.Editions").AsEnumerable().First();

For more details, including a SQL trace, please see the discussion here:

http://stackoverflow.com/questions/7808218/strange-behaviour-with-linq-to-entities-include

How to repeat:
(1) Create a schema and import the attached data.

(2) Make a new .NET project and generate an entity model from the schema.

(3) Execute the code below.

using (var db = new Db()) {
  var author = db.Authors.Include("Books.Editions").First();
  foreach (var book in author.Books) {
    foreach (var edition in book.Editions)
      Response.Write(edition.Id + " - " + edition.Title + "<br />");
  }
}

This code should output:

1 - Some Book First Edition
2 - Another Book First Edition
3 - Another Book Second Edition
4 - Another Book Third Edition

But it outputs (wrongly):

8 - Some Book First Edition
9 - Another Book First Edition

Suggested fix:
Look at the way the Entity Provider handles JOINs.
[18 Oct 2011 20:27] James Gurung
SQL file

Attachment: Dump20111018.sql (application/octet-stream, text), 3.01 KiB.

[15 Nov 2011 9:46] Bogdan Degtyariov
Verified with Connector/NET 6.4.4 as described.
The output is:

8 - Some Book First Edition
9 - Another Book Second Edition
[12 Jan 2012 22:03] Gabriela Martinez Sanchez
This seems to be a problem on how the connector is handling the Eager Loading. If instead is used related objects the output is correct, so there is a workaround for this bug. Anyhow we are working on the fix and we'll notify you when the fix is done.
[8 Mar 2012 19:36] Gabriela Martinez Sanchez
Thank you for your bug report. This issue has already been fixed in the latest released
version of that product, which you can download at

  http://www.mysql.com/downloads/

This fix will appear in versions 6.3.9, 6.4.5 & 6.5.4.
[8 Mar 2012 19:36] Gabriela Martinez Sanchez
Thank you for your bug report. This issue has already been fixed in the latest released
version of that product, which you can download at

  http://www.mysql.com/downloads/

This fix will appear in versions 6.3.9, 6.4.5 & 6.5.4.
[11 Apr 2012 17:50] John Russell
Added to changelog for 6.3.9, 6.4.5, 6.5.5:

In "LINQ to Entity" queries, including a child entity (1-n) and its
entities (n-n) returned the wrong results. For example:

 db.Authors.Include("Books.Editions").AsEnumerable().First();