我有一個現有的數據庫,我試圖通過實體框架4.3訪問。大多數表格和關係都不成問題,但這套表格引起了我幾個似乎無法找到答案的問題。實體框架多個父表
這裏是(濃縮)實體:
客戶
public class Customer
{
public int CustomerID { get; set; }
public string Name { get; set; }
private int addressSourceTypeID = 2;
[NotMapped]
public int AddressSourceTypeID {
get { return addressSourceTypeID; }
set { addressSourceTypeID = value; } }
public virtual ICollection<User> Users { get; set; }
public virtual ICollection<Contract> Contracts { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
合同
public class Contract
{
public int ContractID { get; set; }
public string Name { get; set; }
private int addressSourceTypeID = 4;
[NotMapped]
public int AddressSourceTypeID {
get { return addressSourceTypeID; }
set { addressSourceTypeID = value; } }
public virtual int CustomerID { get; set; }
public virtual Customer Customer { get; set; }
//public virtual ICollection<Address> Addresses { get; set; }
}
地址
public class Address
{
[Key]
public int AddressID { get; set; }
public int AddressSourceTypeID { get; set; }
[ForeignKey("Customer")]
public int SourceKey { get; set; }
public virtual Customer Customer { get; set; }
//public virtual Contract Contract { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
}
我上面的是兩個實體Customer
和Contract
,這兩個實體都可以有子女Address
。目前,Address
實體被設置爲Customer
實體的子項,並且此工作正常,因爲沒有從Address
到Contract
的鏈接。
我曾嘗試在中加入Address
實體,就像我在Customer
實體中所做的那樣,您可以從註釋掉的代碼段中看到。不幸的是,這不起作用,但我並不感到驚訝,因爲在Address
ForeignKey註釋中引用Customer
。我甚至試圖創建Address
實體的特定版本(即CustomerAddress
),但是當多個實體試圖綁定到同一個表時,我得到一個錯誤。
我也嘗試在EF DBContext
中使用ModelBuilder
但是我在這裏的知識是相當有限的,我不知道在這種情況下如何做到這一點。
總體來說,我需要的是以下幾點:
- 客戶實體有子地址的集合。
- 要收集子地址的合同實體。
到地址表中的這些 '父' 表之間的鏈路使用以下:
- 顧客:客戶id =>地址:SourceKey和客戶:AddressSourceTypeID(始終爲2)=>地址:AddressSourceTypeID。
- 合同:ContractID =>地址:SourceKey AND合同:AddressSourceTypeID(始終爲4)=>地址:AddressSourceTypeID。
如果任何人都可以幫助我,或者指出我會朝着正確的方向發展。
非常感謝。
數據庫中的Address表中的SourceKey列是* both *一對多關係的外鍵???這在技術上是允許的,您可以在'SourceKey'上使用兩個'[ForeignKey]'註釋來表示'Customer'和'Contract'導航屬性。但是,這意味着無論何時,通過'SourceKey' FK'Address'引用'Customer''1234'時,還必須有'Contract'1234',反之亦然 - 這很難想象,因爲'Customer'和'契約'通過一對多關係相關,您的密鑰看起來像自動生成的身份。 – Slauma