I'm using the Project() on my IQueryable of "EF Object" to map it to IQueryable of "POCO Model" in the data layer.
The generated SQL statement has 20 LEFT JOINs to the same table. Has anyone seen this behavior?
The object that is being queried has a nested complex objects.
I have the same problem. I don't know what to do about it, but I think I understand what the problem is. In short, Entity Framework duplicates joins in certain query structures, and AutoMapper uses those query structures.
When Entity Framework sees
entity.Relationship.Field1andentity.Relationship.Field2in a linq query, it generates a separate join for each field. for example:might generate the following sql:
Normally, this double-join can be avoided by writing linq like this:
Producing:
Unfortunately, you don't have this control in AutoMapper. When using
.Project().To(), they presumably generate each "select" mapping separately and completely, like in the first example. It's probably possible for them to see which relationships/joins they would want to reuse, but depending on how they build the query up, theletsyntax might be unavailable to them.