I created an object type that has several collection properties that reference elements of the same type. In other words, it reflects a single database table in which rows are randomly grouped so that a row can be displayed in multiple groups.
In the following simplified example, the Person class has Brothers and Sisters collection properties that also reference Person objects:
public class Person { public Person() { Brothers = new Collection<Person>(); Sisters = new Collection<Person>(); } [Key] public string Name { get; set; } public int Age { get; set; } public virtual ICollection<Person> Brothers { get; set; } public virtual ICollection<Person> Sisters { get; set; } }
The Entity Framework seems to consider this a valid model, but interprets it to create a single PersonPersons join table that does not reflect the brother-sister relationship.
I suppose the solution is to use the free API to explicitly map individual join tables for two relationships, but despite extensive experimentation, I was not able to get this to work.
Any suggestions please?
Thanks Tim
entity-framework many-to-many code-first self-reference
Tim coulter
source share