我是 Entity Framework 的新手,我的英语不是很好,如果我写错了,抱歉。我想使用以下示例与 Entity Framework 6 建立一对多关系的单向关联:
public class Person
{
public int personId;
public string Name;
.
.
.
//public ICollection<Phone> Phones { get; set; } //I don't want this nav property
}
public class Phone
{
public int phoneId;
public string number;
public Person myPerson { get; set; }
}
在这个类中,一个人有很多电话,所以一个电话只有一个人(1到1...*),但我想在电话中创建导航属性,而不是在人中。
如何创建与 Fluent API 的关联以进行映射?
最佳答案
使用以下映射
modelBuilder.Entity<Phone>().HasRequired(p => p.myPerson).WithMany();
- HasRequired配置电话所需的关系(即需要有人员 ID)
- WithMany()将关系配置为必需:许多侧没有导航属性的许多
关于entity-framework - Entity Framework 单向关联一对多,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20909240/