Skip to content

Synchronize IncludePaths with ownerEntity on navigation expanding - #39466

Merged
AndriySvyryd merged 3 commits into
dotnet:mainfrom
JaiWey:issue-30458
Oct 31, 2026
Merged

AndriySvyryd merged 3 commits into
dotnet:mainfrom
JaiWey:issue-30458

Conversation

@JaiWey

@JaiWey JaiWey commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #30458

  • I've read the guidelines for contributing and seen the walkthrough
  • I've posted a comment on an issue with a detailed description of how I am planning to contribute and got approval from a member of the team
  • The code builds and tests pass locally (also verified by our automated build checks)
  • Commit messages follow this format:
        Summary of the changes
        - Detail 1
        - Detail 2

        Fixes #bugnumber
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

When an owned entity contains another navigation and the Include clause after Where, the joined table is not generated because the IncludePaths of owned Entity is empty

private Expression ExpandIncludesHelper(Expression root, EntityReference entityReference, INavigationBase? previousNavigation)
{
var result = root;
var convertedRoot = root;
foreach (var (navigationBase, includeTreeNode) in entityReference.IncludePaths)
{
if (!navigationBase.IsCollection
&& previousNavigation?.Inverse == navigationBase)
{

If put the Include before Where, the ownedEntity get its includePaths from parent OwnedEntity during the expandingof lambda body x.Information.Boolean from here

if (entityReference.IncludePaths.TryGetValue(navigation, out var includePath))
{
ownedEntityReference.IncludePaths.Merge(includePath);
}

if Include after Where, the includePath from parent owner entity is null during Where clause lambda expanding, hence the includePath of ownedEntity is null. To solve this, add another merge when access the ownedExpansion from ForeignKeyExpansionMap
if (entityReference.ForeignKeyExpansionMap.TryGetValue(
(navigation.ForeignKey, navigation.IsOnDependent), out var ownedExpansion))
{
if (ownedExpansion is OwnedNavigationReference navigationReference && entityReference.IncludePaths.TryGetValue(navigation, out var entityReferenceIncludePath))
{
navigationReference.EntityReference.IncludePaths.Merge(entityReferenceIncludePath);
}
return ownedExpansion;
}

Add two test cases for the different Include/Where order

… the include navigation expand after original ownedEntity is created

Fixes dotnet#30458
@JaiWey
JaiWey requested a review from a team as a code owner October 30, 2026 10:43
@JaiWey JaiWey changed the title Synchronize IncludePaths with ownerEntity on navigation expansion Synchronize IncludePaths with ownerEntity on navigation expanding Oct 30, 2026
@AndriySvyryd
AndriySvyryd requested a balanced review from Copilot October 30, 2026 19:47

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The shared tests still pass when the include is dropped and Information.Nav is null; both operator orders need non-null navigation assertions to detect the original regression.

1 open issue
What changed in this PR

Addresses #30458 by synchronizing include paths when EF Core reuses a cached owned-navigation expansion, preserving nested includes placed after Where.

Changes:

  • Merges the owner’s current include paths into cached owned-navigation references.
  • Adds synchronous and asynchronous tests for both Include/Where orders.
  • Adds SQL Server baselines verifying the navigation join for both orders.
File Description
test/​EFCore.SqlServer.FunctionalTests/​Query/​OwnedEntityQuerySqlServerTest.cs Adds SQL baselines for both operator orders.
test/​EFCore.Relational.Specification.Tests/​Query/​OwnedEntityQueryRelationalTestBase.cs Adds shared regression tests and a seeded owned-entity model.
src/​EFCore/​Query/​Internal/​NavigationExpandingExpressionVisitor.ExpressionVisitors.cs Updates include paths when reusing cached owned-navigation expansions.

Review effort: Balanced

@AndriySvyryd AndriySvyryd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@AndriySvyryd
AndriySvyryd merged commit 8ece311 into dotnet:main Oct 31, 2026
18 of 19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Different Include/Where order leads to different SQL when using owned entity

3 participants