Skip to content

Code example calls a method with lambdas as parameters, where the lambda parameter names are the opposite of what the method labels them. #4846

Open
@jphorv-bdo

Description

@jphorv-bdo

Type of issue

Other (describe below)

Description

In the docs about how to configure many-to-many relationships with a class for a join entity:

https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#many-to-many-with-class-for-join-entity

The code sample looks like this:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Post>()
        .HasMany(e => e.Tags)
        .WithMany(e => e.Posts)
        .UsingEntity<PostTag>(
            l => l.HasOne<Tag>().WithMany().HasForeignKey(e => e.TagId),
            r => r.HasOne<Post>().WithMany().HasForeignKey(e => e.PostId));
}

The call to UsingEntity<PostTag>() has those 2 lambdas, the first one with a parameter of l and the 2nd with a parameter named r, suggesting "left" and "right".

The problem is that the UsingEntity method signature has those reversed, where "right" is first, then "left". Like this: UsingEntity(Func<...> configureRight, Func<...> configureLeft) The sample suggests the parameter order is (left, right) when in actuality it is (right, left).

Yes, type checking should prevent the user from doing something invalid, but it may confuse developer as to what is considered "left" and what is considered "right".

Page URL

https://learn.microsoft.com/en-us/ef/core/modeling/relationships/many-to-many#many-to-many-with-class-for-join-entity

Content source URL

https://github.com/dotnet/EntityFramework.Docs/blob/main/entity-framework/core/modeling/relationships/many-to-many.md

Document Version Independent Id

9234f104-802f-8ee0-8a54-ff3b8f49857e

Article author

@ajcvickers

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions