我已经创建了具有指向同一类型的多个项目集合属性的实体类型映射。换句话说,它反映了一个单独的数据库表,其中行被任意分组,使得一行可以出现在多个组中。实体框架代码优先:如何将多个自引用许多一对多的关系
在下面的简单示例中,Person
类有Brothers
和Sisters
集合属性也引用Person
实体:
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; }
}
实体框架似乎认为这是一个有效的模式,但其解释创建单身PersonPersons
连接表,未能体现兄妹关系的分离。
我认为,解决办法是用流利的API显式地映射单独的连接表的两个关系,但是,尽管有大量的实验,我一直无法得到这个工作。
有什么建议吗?
感谢, 添
你有没有试过,HasMany(p => p.Brothers).WithMany(b => b.Persons).Map(m => m.Table(“Person_Brothers”);还没有尝试过这种类型的关系,但那我们如何U映射多对多时EF不会合作。 – trevorc 2014-11-14 14:09:03