Skip to content

AutoInclude not working with unidirectional many-to-many #31380

Closed

Description

@Moerup

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.

A small test repo to reproduce the bug can be found here:
https://github.com/Moerup/ManyToManyAutoInclude

Book entity:

public class Book
{
    public int Id { get; set; }

    public string Name { get; set; }
}

Library entity:

public class Library
{
    public int Id { get; set; }

    public string Name { get; set; }

    public List<Book> Books { get; set; } = new();
}

Library configuration:

    public void Configure(EntityTypeBuilder<Library> builder)
    {
        // This does NOT include books automatically like expected
        builder.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-many
        builder.HasMany(x => x.Books).WithMany();
    }

Queries:

        // No related books returned
        var libraryWithAutoInclude = dbContext.Libraries.First();

        // Related books returned
        var libraryWithSpecificInclude = 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

Activity

  1. ajcvickers commented on Aug 2, 2023

    @ajcvickers
    Contributor

    @Moerup As a workaround, configure many-to-many first. For example:

            builder.HasMany(x => x.Books).WithMany();
            builder.Navigation(x => x.Books).AutoInclude();

    Note for triage: this works with one-to-many, presumably because the navigation doesn't later change to a skip navigation.

  2. Moerup commented on Aug 3, 2023

    @Moerup
    Author

    Thanks for the workaround, seems to do the trick 👍

  3. added this to the Backlog milestone on Aug 6, 2023
  4. added theissue type on Jun 15, 2025
  5. AsadIqbal-Dev commented on Nov 2, 2026

    @AsadIqbal-Dev
    Contributor

    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.

  6. added a commit that references this issue on Nov 13, 2026
    c685570
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions