question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Projecting Complex Models in Entity Framework Causes Join Duplication

See original GitHub issue

As described in this stack overflow question and its answer, trying to map an EF entity class along with relationships results in unnecessary extra join clauses created for each related field mapped.

for example:

from entity in dbSet
select new { entity.Relationship.Field1, entity.Relationship.Field2 }

might generate the following sql:

SELECT [Extent2].[Field1], [Extent3].[Field2]
FROM EntityTable AS [Extent1]
JOIN RelatedTable AS [Extent2] ON [Extent1].[RelationshipID] = [Extent2].[ID]
JOIN RelatedTable AS [Extent3] ON [Extent1].[RelationshipID] = [Extent3].[ID]

Essentially, every time entity.Relationship is referenced, a new join will be generated.

Admittedly, this is potentially a shortcoming with Entity Framework. However, I suspect they generate the extra joins because the way the query breaks down into function expressions prevents them from knowing when two usages of entity.Relationship are really the same join.

I can manually code around this when I’m writing my own queries, but when using AutoMapper’s ProjectTo I don’t have the ability. Since a projection would be able to re-use the join, I’m hoping this can be worked around in AutoMapper, too.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
TylerCarlson1commented, Dec 19, 2014

Looks like EF 6.1.2 has fixed the issue, or at the very least improved the query in some cases.

0reactions
lock[bot]commented, May 8, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework Include OrderBy random generates ...
But it does not cause entities to get duplicated in results. Beware: If your query ordering does not guarantees uniqueness of your entities...
Read more >
Complex Query Operators - EF Core
The LINQ Join operator allows you to connect two data sources based on the key selector for each source, generating a tuple of...
Read more >
Single vs. Split Queries - EF Core
It's worth noting that unlike cartesian explosion, the data duplication caused by JOINs isn't typically significant, as the duplicated data ...
Read more >
Entity Framework Core 5 - Pitfalls To Avoid and Ideas to Try
In this post, we'll look at some pitfalls and ideas EF Core users like yourself may want to consider when developing an application....
Read more >
How To Deal with Duplicate Entries Using SQL
Regardless of how duplicates were created, the first step is to identify them within your data table. The most difficult part here is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found