I have created an entity type that has multiple collection properties that reference items of the same type. In other words, it reflects a single database table in which the rows are arbitrarily grouped, such that a row may appear in multiple groups.
In the following simplified example, the Person
class has Brothers
and Sisters
collection properties that also reference Person
entities:
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; }
}
Entity Framework seems to think that this is a valid model, but interprets it to create a single PersonPersons
join table, which fails to reflect the separation of brother and sister relationships.
I assume the solution is to use the fluent API to explicitly map separate join tables for the two relationships but, despite extensive experimentation, I have been unable to get this to work.
Any suggestions please?
Thanks, Tim
By adding this in the DbContext.OnModelCreating method:
UPDATE Added table-naming map according to nameEqualsPNamePrubeGoldberg's comment above:
I got this unit test to pass
That also creates the link tables you're talking about.