我已经注意到为早期测试版的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; }
}
映射所有内容的方式在早期版本中发生了很大变化。如何以一对一的关系映射这两个实体?
答案 0 :(得分:4)
EF7 rc1-final已更改。
$string = '2 7 6 9 11';
$string_parts = explode(' ', $string);
$pattern = array('/^1$/','/^2$/','/^3$/','/^4$/','/^5$/','/^6$/','/^7$/','/^8$/','/^9$/','/^10$/','/^11$/','/^12$/','/^13$/','/^14$/');
$result = [];
foreach ($string_parts as $string_part) {
$result[] = preg_replace($pattern, $companyTypes, $string_part);
}
$order->company_type = implode(' ', $result);
UPD:
modelBuilder.Entity<FirstEntity>()
.HasOne(q => q.SecondEntity)
.WithMany()
.HasForeignKey(q => q.SecondEntityID);
答案 1 :(得分:-2)
Beta 7在很大程度上依赖于使用Fluent API的Reference
方法来执行任何类型的外键关系,您首先要选择FK关系中涉及的导航属性。然后,由于这是一对一的关系,因此您使用InverseReference
关系来指定这将适用于关系另一侧的哪个导航属性。只有这样,您才能真正开始使用ForeignKey
和PrincipalKey
方法指定所涉及的密钥。
总而言之,这是我使用的解决方案:
modelBuilder.Entity<SecondEntity>
.Reference(s => s.FirstEntity)
.InverseReference(f => f.SecondEntity)
.ForeignKey(typeof(SecondEntity), "FirstEntityID")
.PrincipalKey(typeof(FirstEntity), "SecondEntityID");