Skip to content

Keep navigation configuration when a navigation becomes a skip navigation - #39502

Merged
AndriySvyryd merged 2 commits into
dotnet:mainfrom
AsadIqbal-Dev:fix/autoinclude-unidirectional-many-to-many
Nov 13, 2026
Merged

AndriySvyryd merged 2 commits into
dotnet:mainfrom
AsadIqbal-Dev:fix/autoinclude-unidirectional-many-to-many

Conversation

@AsadIqbal-Dev

Copy link
Copy Markdown
Contributor
  • 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
  • Tests for the changes have been added (for bug fixes / features)
  • Code follows the same patterns and style as existing code in this repo

Fixes #31380

At the time of Navigation(x => x.Books).AutoInclude(), Books is a one-to-many navigation found by convention. HasMany(x => x.Books).WithMany() then removes that foreign key with its navigation and creates a new skip navigation, so the setting is lost. It only works today if the many-to-many is configured first.

WithLeftManyNavigation and WithRightManyNavigation now copy the explicitly configured annotations from the removed navigation to the new skip navigation. That's the same rule InternalSkipNavigationBuilder.Attach uses when it moves a skip navigation. It keeps AutoInclude, EnableLazyLoading, UsePropertyAccessMode and custom annotations. I copy them in a small helper because MergeAnnotationsFrom only accepts the builder's own metadata type.

Added to ModelBuilderTest.ManyToManyTestBase:

  • Navigation_configuration_is_kept_when_navigation_becomes_skip_navigation
  • Navigation_configuration_is_kept_when_inverse_navigation_becomes_skip_navigation

Both fail without the change. EFCore.Tests, EFCore.Relational.Tests and the in-memory model builder tests pass with it.

Two things I left out to keep this to the reported bug:

  • An explicit HasField isn't annotation based, so it isn't copied. Attach handles it separately.
  • WithOneBuilder does the opposite conversion, from a skip navigation to a foreign key navigation, and drops the configuration in the same way.

I can add either one here if you'd like.

…tion

- WithMany removes the foreign key and its navigation, then creates a new skip navigation, which lost AutoInclude and other explicit configuration
- Copy the explicitly configured annotations to the skip navigation, for both the left and the right side

Fixes dotnet#31380
Cosmos maps a target found by convention as owned, so the navigation doesn't reach the path this test covers.
@AndriySvyryd
AndriySvyryd requested a lite review from Copilot November 3, 2026 01:47
}
}

private static void CopyExplicitAnnotations(Navigation? navigation, InternalSkipNavigationBuilder skipNavigationBuilder)

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.

Rename to CopyExplicitConfiguration

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.

🟢 Approval recommended

The change transfers explicit navigation annotations and adds regression coverage for both conversion directions, with no active review issues identified.

No open issues 🎉

What changed in this PR

This pull request preserves explicitly configured navigation annotations when a convention-created foreign-key navigation becomes a many-to-many skip navigation.

Changes:

  • Copies explicit annotations, including AutoInclude, lazy-loading settings, property access mode, and custom annotations.
  • Adds regression tests for conversions of both the primary and inverse navigations.
  • Explicit HasField configuration remains unpreserved and requires follow-up.

This pull request requires changes.

File Summary
test/​EFCore.Specification.Tests/​ModelBuilding/​ModelBuilderTest.ManyToMany.cs Adds regression tests covering preservation of navigation configuration in both conversion directions.
src/​EFCore/​Metadata/​Builders/​CollectionNavigationBuilder.cs Transfers explicit navigation annotations when creating skip navigations from foreign-key navigations.

Review effort: Lite

@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 c685570 into dotnet:main Nov 13, 2026
19 checks passed
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Nov 14, 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.

AutoInclude not working with unidirectional many-to-many

3 participants