Skip to content

Documentation should include how to map many to many relationship with self-referencing relations #3810

Closed

Description

@prashantchoudhary

Many to many relations don't work by default, if it is a reference between same entity. And implementing this is not trivial. So I believe the work around should be included in documentation.

//Person class
public partial class Person
{
    public Guid Id {get; private set;}
    private readonly List<Friendship> _allFriends = new();
    public IReadOnlyCollection<Friendship> AllFriends => _allFriends;
}

//Friendship class
public class Friendship
{
    public Guid Id { get; set; }
    public Person Friend { get; init; }
    public Friendship(Person friend)
    {
        Friend = friend;
    }
}
//DbContext
modelBuilder.Entity<Friendship>().HasOne(friendship => friendship.Friend).WithMany();
modelBuilder.Entity<Person>().HasMany(p => p.Friends).WithOne(f => f.Friend);

Document Details

⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

Activity

  1. ajcvickers commented on Apr 19, 2022

    @ajcvickers
    Contributor

    @prashantchoudhary

    Many to many relations don't work by default, if it is a reference between same entity

    Please provide more details on this. Ideally, attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

  2. prashantchoudhary commented on Apr 19, 2022

    @prashantchoudhary
    Author

    Adding the link to Gist.
    Link to the original issue with workaround: dotnet/efcore#27785

  3. ajcvickers commented on Apr 19, 2022

    @ajcvickers
    Contributor

    @prashantchoudhary So are you attempting to use EF mappings to create a many-to-many relationship between Person and Person, using Friendship as the join table? Or are you attempting to create two one-to-many relationships to simulate a many-to-many--i.e. not using EF's many-to-many mapping support? Also, are you attempting to use the same FK and navigation for both directions of the many-to-many? (I'm trying to understand your code, but I'm finding it really difficult to understand the intention here.)

  4. prashantchoudhary commented on Apr 19, 2022

    @prashantchoudhary
    Author

    I am trying to represent friendship, Where One person can have many Friendships. So it is Many to many relation, but not direct.

    Person--> HasMany -FriendShip
    Friendship --> HasOne-Person

    So yes, it is many to many relations between Persons using Friendship as the Join table.
    Navigation is Unidirectional. So if two Persons are friends then there will be two entries in the Frienship Table..
    Sorry if my code was not readable. Will see if it can be refactored.

  5. slch commented on Apr 21, 2022

    @slch
    Contributor

    In docs there are no examples of
    Self-referencing relationship: A relationship in which the dependent and the principal entity types are the same.
    while it being mentioned.
    Addition of such examples with self-referencing X-to-X relationships would close this issue.

  6. ajcvickers commented on Apr 25, 2022

    @ajcvickers
    Contributor

    @prashantchoudhary I think the main issue here is that EF cannot re-use the same navigation property for multiple relationships. That being said, we will document the normal way to do self-referencing many-to-many relationships.

  7. self-assigned this
    on Apr 25, 2022
  8. added this to the 7.0.0 milestone on Apr 25, 2022
  9. added 8 commits that reference this issue on Feb 25, 2023
    e20432f
    671a66c
    94ff962
    f33e5ed
    c7b38aa
    93b4c5e
    1ba858c
    bb39d2d
  10. added a commit that references this issue on Mar 19, 2023
    9a1e6f2
  11. added 2 commits that reference this issue on Mar 28, 2023
    f5d7299
    dd7c736
  12. modified the milestones: 7.0.0, 8.0.0 on Mar 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions