Repository navigation
Keep navigation configuration when a navigation becomes a skip navigation - #39502
Merged
AndriySvyryd merged 2 commits intoNov 13, 2026
Merged
AndriySvyryd merged 2 commits into
AndriySvyryd merged 2 commits into
Conversation
…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
reviewed
Nov 3, 2026
| } | ||
| } | ||
|
|
||
| private static void CopyExplicitAnnotations(Navigation? navigation, InternalSkipNavigationBuilder skipNavigationBuilder) |
Member
There was a problem hiding this comment.
Rename to CopyExplicitConfiguration
Contributor
There was a problem hiding this comment.
🟢 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
HasFieldconfiguration 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
approved these changes
Nov 13, 2026
AndriySvyryd
left a comment
Member
There was a problem hiding this comment.
Thanks for your contribution!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #31380
At the time of
Navigation(x => x.Books).AutoInclude(),Booksis 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.WithLeftManyNavigationandWithRightManyNavigationnow copy the explicitly configured annotations from the removed navigation to the new skip navigation. That's the same ruleInternalSkipNavigationBuilder.Attachuses when it moves a skip navigation. It keepsAutoInclude,EnableLazyLoading,UsePropertyAccessModeand custom annotations. I copy them in a small helper becauseMergeAnnotationsFromonly accepts the builder's own metadata type.Added to
ModelBuilderTest.ManyToManyTestBase:Navigation_configuration_is_kept_when_navigation_becomes_skip_navigationNavigation_configuration_is_kept_when_inverse_navigation_becomes_skip_navigationBoth fail without the change.
EFCore.Tests,EFCore.Relational.Testsand the in-memory model builder tests pass with it.Two things I left out to keep this to the reported bug:
HasFieldisn't annotation based, so it isn't copied.Attachhandles it separately.WithOneBuilderdoes 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.