【发布时间】:2015-08-17 21:20:47
【问题描述】:
我以前从未使用过导航属性,所以我试图了解它们是如何工作的。作为偏好,我更喜欢通过 fluent API 映射它们。有人可以向我解释如何使用 fluent API 建立这种关系吗?
public class FooA
{
[Key]
public int FooAID {get;set;}
[Required]
public String NameA {get;set;}
}
public class FooB
{
[Key]
public int FooBID {get;set;}
[Required]
public String NameB {get;set;}
public int? FooA_FK1 {get;set;}
public int? FooA_FK2 {get;set;}
[ForeignKey("FooA_FK1")]
public virtual FooA Nav_FK1{get;set;}
[ForeignKey("FooA_FK2")]
public virtual FooA Nav_FK2{get;set;}
}
/*
Note that FooB has TWO NULLABLE (Optional?) references to FooA objects.
Also, the foreign key names don't follow the convention for EF.
I want to understand the fluent API used to construct this type of relationship.
I have been able to use fluent API to get this set up provided that the items
are required. When I tried using the .HasOptional() method, I got an exception.
*/
【问题讨论】:
-
关系是 1:1?
标签: c# entity-framework ef-fluent-api