Entity Framework Code First: how to map multiple self-referencing many-to-many relationships

4.2k views Asked by At

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

1

There are 1 answers

1
Daniel Persson On

By adding this in the DbContext.OnModelCreating method:

UPDATE Added table-naming map according to nameEqualsPNamePrubeGoldberg's comment above:

modelBuilder.Entity<Person>().HasMany(x => x.Brothers).WithMany()
    .Map(x => x.ToTable("Person_Brothers"));
modelBuilder.Entity<Person>().HasMany(x => x.Sisters).WithMany()
    .Map(x => x.ToTable("Person_Sisters"));

I got this unit test to pass

[TestMethod]
public void TestPersons()
{
    var brother = new Person() { Name = "Brother 1", Age = 10 };
    var sister = new Person() { Name = "Sister 1", Age = 12 };
    var sibling = new Person() { Name = "Sibling 1", Age = 18 };
    sibling.Brothers.Add(brother);
    sibling.Sisters.Add(sister);

    using (var db = new MyDatabase())
    {
        db.Persons.Add(brother);
        db.Persons.Add(sister);
        db.Persons.Add(sibling);

        db.SaveChanges();
    }

    using (var db = new MyDatabase())
    {
        var person = db.Persons
            .Include(x => x.Sisters)
            .Include(x => x.Brothers)
            .FirstOrDefault(x => x.Name.Equals(sibling.Name));

        Assert.IsNotNull(person, "No person");
        Assert.IsTrue(person.Brothers.Count == 1, "No brothers!");
        Assert.IsTrue(person.Sisters.Count == 1, "No sisters");
    }
}

That also creates the link tables you're talking about.