1

I am playing with Entity Framework (code first) and want my model to reference itself... which is clearly explained in several places and it fairly straightforward:

public class Person{
    public ICollection<Person> People { get; set; }
}

I want this relationship to work bi-directionally - I want to retrieve this relationship in a similar or identical manner regardless of which person in the relationship I'm querying for.

I also want to describe that relationship with a string.

I am not readily finding information on this sort of relationship. Perhaps there's a specific name for this kind of relationship I am unaware of. Based on other frameworks I've used, I would expect to build something sort of like:

public class Person{
    public ICollection<PeoplePeople> RelatedPeople { get; set; }
}

public class PeoplePeople{
    public Person personOne { get; set; } 
    public Person personTwo { get; set; }
    public string Relationship { get; set; } 
}

Is this the right way to go about this relationship in Entity Framework?

Other thoughts

Perhaps as an array of people?

public class PeoplePeople{
    public Person[] people { get; set; } 
    public string Relationship { get; set; } 
}

Not sure how EF would handle this in the DB though, might not be what is needed.

Randy Hall
  • 7,716
  • 16
  • 73
  • 151
  • You should be covered on Inheritance in the EF model as following link: https://learn.microsoft.com/en-us/ef/core/modeling/relational/inheritance – Ha Hoang Jun 12 '17 at 04:14
  • This might help: https://stackoverflow.com/questions/10807900/how-to-store-bidirectional-relationships-in-a-rdbms-like-mysql – Frank Fajardo Jun 15 '17 at 23:52
  • @FrankFajardo Definitely good information for anyone new to database design. Approach 1 is clearly defined in many of the EF docs an tutorials, I really need to know how to correctly implement approach 2 in EF – Randy Hall Jun 16 '17 at 13:22

0 Answers0