Entity Framework 6 Refresher: Many To Many Referencing Self
1 minute read
Occasionally a many-to-many relationship needs to be established with an entity pointing to itself; In the case of the small domain we are working with, we can examine this with the concept of a Family - Person referencing Person.
In this case, we are going to need mapping information as the default migration built for this makes each person belong to one, and only one family (we’re doing a learning exercise here, ok?)
A mapping that you might think would work will cause an error The navigation property 'Family' declared on type 'LearnMappings.Person' cannot be the inverse of itself.
In this case we need to leave WithMany empty. We also need to setup the Map information as the defaults will be kind of funky (PersonPerson with columns called Person_PersonId1 and Person_PersonId)