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
Documentation should include how to map many to many relationship with self-referencing relations #3810
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.
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 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.)
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.
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.
@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.
Activity
ajcvickers commentedon Apr 19, 2022
@prashantchoudhary
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 commentedon Apr 19, 2022
Adding the link to Gist.
Link to the original issue with workaround: dotnet/efcore#27785
ajcvickers commentedon Apr 19, 2022
@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 commentedon 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 commentedon 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 commentedon Apr 25, 2022
@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.
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation
Major overhaul of the "relationships" documentation