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

蹲街弑〆低调 提交于 2019-11-27 04:45:51

问题


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:


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.



来源:https://stackoverflow.com/questions/26930715/entity-framework-code-first-how-to-map-multiple-self-referencing-many-to-many-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!