You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
AutoInclude not working with unidirectional many-to-many #31380
Using AutoInclude on navigation property when using a unidirectional many to many does not include the related entities.
Explicitly including them in the Linq query works as expected.
publicvoidConfigure(EntityTypeBuilder<Library>builder){// This does NOT include books automatically like expectedbuilder.Navigation(x =>x.Books).AutoInclude();// Unidirectional many to many config => https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#unidirectional-many-to-manybuilder.HasMany(x =>x.Books).WithMany();}
Queries:
// No related books returnedvarlibraryWithAutoInclude=dbContext.Libraries.First();// Related books returnedvarlibraryWithSpecificInclude=dbContext.Libraries.Include(x =>x.Books).First();
Include provider and version information
EF Core version: 7.0.9 and 8.0.0-preview.6.23329.4
Database provider: Microsoft.EntityFrameworkCore.Sqlite and Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7
Operating system: Windows and Linux (tested in docker)
IDE: Visual Studio 2022 17.6.5
The configuration is lost in CollectionNavigationBuilder.WithLeftManyNavigation. At the time of Navigation(x => x.Books).AutoInclude(), Books is a one-to-many navigation found by convention. WithMany() then removes that foreign key with its navigation and creates a new skip navigation, and the eager load annotation goes away with the old navigation.
I'd copy the explicitly configured annotations to the new skip navigation, the same way InternalSkipNavigationBuilder.Attach does when it moves a skip navigation. I'll send a PR with tests.
Activity
ajcvickers commentedon Aug 2, 2023
@Moerup As a workaround, configure many-to-many first. For example:
Note for triage: this works with one-to-many, presumably because the navigation doesn't later change to a skip navigation.
Moerup commentedon Aug 3, 2023
Thanks for the workaround, seems to do the trick 👍
AsadIqbal-Dev commentedon Nov 2, 2026
The configuration is lost in
CollectionNavigationBuilder.WithLeftManyNavigation. At the time ofNavigation(x => x.Books).AutoInclude(),Booksis a one-to-many navigation found by convention.WithMany()then removes that foreign key with its navigation and creates a new skip navigation, and the eager load annotation goes away with the old navigation.I'd copy the explicitly configured annotations to the new skip navigation, the same way
InternalSkipNavigationBuilder.Attachdoes when it moves a skip navigation. I'll send a PR with tests.Keep navigation configuration when a navigation becomes a skip naviga…