实体框架代码优先:如何映射多个自引用多对多关系 [英] Entity Framework Code First: how to map multiple self-referencing many-to-many relationships
问题描述
我创建了一个实体类型,该实体类型具有多个引用相同类型项目的集合属性.换句话说,它反映了一个数据库表,其中的行被任意分组,这样一行就可以出现在多个组中.
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.
在下面的简化示例中,Person
类具有Brothers
和Sisters
集合属性,它们也引用Person
实体:
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; }
}
实体框架似乎认为这是一个有效的模型,但是将其解释为创建单个PersonPersons
连接表,该表无法反映兄弟姐妹关系的分离.
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.
我认为解决方案是使用fluent API为两个关系显式映射单独的联接表,但是尽管进行了广泛的实验,我仍然无法使它起作用.
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.
有什么建议吗?
谢谢, 蒂姆
推荐答案
通过在DbContext.OnModelCreating方法中添加它:
By adding this in the DbContext.OnModelCreating method:
更新根据上面的nameEqualsPNamePrubeGoldberg的评论添加了表命名映射:
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"));
我通过了这个单元测试
[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.
这篇关于实体框架代码优先:如何映射多个自引用多对多关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!