我注意到一些问题已经在 EF 7 的早期测试版中解决了这个问题(例如 here ),但我还没有看到它在 Beta 7 中得到解决,所以这里是:
我有 2 个实体,简化如下:
public class FirstEntity
{
public int FirstEntityID { get; set; }
/*
Other fields here
*/
public int? SecondEntityID { get; set; }
public SecondEntity SecondEntityProperty { get; set; }
}
public class SecondEntity
{
public int FirstEntityID { get; set; }
/*
Other fields here
*/
public FirstEntity FirstEntityProperty { get; set; }
}
与早期版本相比,映射所有内容的方式发生了很大变化。如何将这两个实体映射为一对一关系?
最佳答案
EF7 rc1-final 已更改。
modelBuilder.Entity<FirstEntity>()
.HasOne(q => q.SecondEntity)
.WithMany()
.HasForeignKey(q => q.SecondEntityID);
更新:
modelBuilder.Entity<FirstEntity>()
.HasOne(q => q.SecondEntity)
.WithOne(v => v.FirstEntity)
.HasForeignKey<FirstEntity>(q => q.SecondEntityID);
关于c# - EF 7(Beta 7)一对一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32530795/