好的,這應該很簡單,但我有麻煩。我有兩個類的區域和位置。一個地區可以有多個地點,但一個地區只能在一個地區。簡單的東西。這是我有:EF代碼第一關係的故障
Location Region
--------- -------
int LocationID int RegionID
string Name string Name
string BlahBlah List<Location> Locations
string OtherStuff
int RegionID
Region Region
現在我需要添加一個家庭辦公室到一個地區。家庭辦公室也是一個位置。實體框架似乎困惑,所以我顯然做錯了事情。這是我想要做的:
Location Region
--------- -------
int LocationID int RegionID
string Name string Name
string BlahBlah List<Location> Locations
string OtherStuff int HomeOfficeID
int RegionID Location HomeOffice
Region Region
我在這裏錯過了什麼?
如果你不喜歡我寫上面這裏是類:
public class Region
{
public int RegionID { get; set; }
public string Name { get; set; }
public int HomeOfficeID { get; set; }
public virtual Location HomeOffice { get; set; }
public virtual List<Location> Locations { get; set; }
}
public class Location
{
public int LocationID { get; set; }
public string Name { get; set; }
public int RegionID { get; set; }
public virtual Region Region { get; set; }
}
我認爲在大多數領域中'HomeOffice'可能不僅僅是一個位置,儘管位置可能是它的一個屬性。 – Alex
即使這樣也不能解決循環外鍵依賴問題。 – Aron
我想如果我有一個具有區域和位置屬性的HomeOffice類,它可能變得更容易。 – georgewillow