Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ThenInclude ignored and re-joined with all columns #7144

Closed
justdmitry opened this issue Nov 28, 2016 · 3 comments
Closed

ThenInclude ignored and re-joined with all columns #7144

justdmitry opened this issue Nov 28, 2016 · 3 comments

Comments

@justdmitry
Copy link

Model classes (simplified)

public class Worklog {
    public Guid Id { get; set; }
    public Guid ContactFieldValueId { get; set; }
    [ForeignKey("ContactFieldValueId")]
    public virtual ContactFieldValue ContactFieldValue { get; set; }
    // some other columns
}

public class ContactFieldValue {
    public Guid Id { get; set; }
    public Guid FieldValueId { get; set; }
    [ForeignKey("FieldValueId")]
    public virtual FieldValue FieldValue { get; set; }
    // some other columns
}

public class FieldValue {
    public Guid Id { get; set; }
    public string Value { get; set; }
    // some other columns
}

And I making a query:

var worklogs = await db.Worklogs
    .Where(x => worklogIds.Contains(x.Id)) // it's List<Guid> with ~1000 values
    .Include(x => x.ContactFieldValue)
    .ThenInclude(cf => cf.FieldValue)
    .Select(x => new
    {
        x.Id, // and some other fields from Worklogs
        ContactFieldValueFieldValue = x.ContactFieldValue.FieldValue.Value
    })
    .OrderBy(x => x.Time)
    .ThenBy(x => x.Id)
    .ToListAsync();

So, I'm selecting some fields from first (Worklog) and one from third (FieldValue) entities.

The issue

I have two problems:

  1. I see warning in logs: The Include operation for navigation: 'x.ContactFieldValue.FieldValue' was ignored because the target navigation is not reachable in the final query results. - this is not true, because I'm selecting Value from it.

  2. Generated SQL SELECT include all columns from ContactFieldValue and all from FiledValue - despite I requested only one:

SELECT 
    [x].[Id], 
    [x].[Comment], [x].[ContactFieldValueId], [x].[IssueId], [x].[ScriptVersionId], [x].[Time], [x].[UserId], 
    [x.ContactFieldValue].[Id], 
    [x.ContactFieldValue].[FieldValueId], 
    // all columns from [x.ContactFieldValue] - why???
    [x.ContactFieldValue.FieldValue].[Id], 
    [x.ContactFieldValue.FieldValue].[Value],
    // all other columns from FieldValue - why???
      FROM [IssueWorklogs] AS [x]
      LEFT JOIN [ContactFieldValues] AS [x.ContactFieldValue] ON [x].[ContactFieldValueId] = [x.ContactFieldValue].[Id]
      LEFT JOIN [FieldValues] AS [x.ContactFieldValue.FieldValue] ON [x.ContactFieldValue].[FieldValueId] = [x.ContactFieldValue.FieldValue].[Id]
      WHERE [x].[Id] IN ('e78183fb-f089-4f99-aaf3-0252679000ae', ...)

Further technical details

EF Core version: Microsoft.EntityFrameworkCore.SqlServer 1.1.0
Operating system: Win 8,
Framework: net461

@maumar
Copy link
Contributor

maumar commented Nov 28, 2016

The Included are not needed in this case, they would only be useful if you projected Worklog entities themselves. If you access navigation as part of a query (in filter, projection etc) EF will automatically add all the joins it needs to perform the query.

@maumar
Copy link
Contributor

maumar commented Nov 28, 2016

Including all columns of entities is due to LEFT JOIN, it's a known issue tracked here: #6647

@justdmitry
Copy link
Author

Got it.
Yes, this is duplicate of #6647

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants