Skip to content

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

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
prashantchoudhary opened this issue Apr 7, 2022 · 6 comments · Fixed by #4269

Comments

@prashantchoudhary
Copy link

prashantchoudhary commented Apr 7, 2022

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.

@ajcvickers
Copy link
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.

@prashantchoudhary
Copy link
Author

prashantchoudhary commented Apr 19, 2022

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

@ajcvickers
Copy link
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.)

@prashantchoudhary
Copy link
Author

prashantchoudhary commented Apr 19, 2022

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.

@slch
Copy link
Contributor

slch commented Apr 21, 2022

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.

@ajcvickers
Copy link
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.

@ajcvickers ajcvickers self-assigned this Apr 25, 2022
@ajcvickers ajcvickers added this to the 7.0.0 milestone Apr 25, 2022
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Feb 25, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Mar 19, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Mar 28, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
ajcvickers added a commit that referenced this issue Mar 30, 2023
Fixes #4145
Fixes #4053
Fixes #3900
Fixes #3810
Fixes #3668
Fixes #3648
Fixes #3282
Fixes #3282
Fixes #2924
Fixes #2807
Fixes #2494
Fixes #2321
Fixes #2266

Part of #3996
@ajcvickers ajcvickers modified the milestones: 7.0.0, 8.0.0 Mar 30, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants