1
我有三個enitiesEnity框架外鍵
public class Customer
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
[Key]
public int Customerid { get; set; }
public string Name { get; set; }
public string email { get; set; }
public string Phoneno { get; set; }
}
和第二Enitity
public class Merchant
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Merchantid { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string MerchantName { get; set; }
[Required]
[RegularExpression("^[\\p{L} .-]+$", ErrorMessage = "Use letters only please")]
public string BusinessName { get; set; }
[Required]
public string OfficePhno { get; set; }
}
public class Transaction
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int TransactionId { get; set; }
public int Merchantid {get; set;}
public int Customerid {get; set;}
public int Amount { get; set; }
public Customer customer {get; set;}
public Merchant merchant {get; set;}
}
在上述交易表
有兩個ID商家ID和用戶ID,我想這對客戶和商戶的外鍵表, 請解釋我如何添加外鍵約束我經歷了很多例子,但無法得到這個答案
d另一個疑問是,如果我添加客戶類型內幕交易表將我能夠在實體 框架
我試圖通過follwing代碼
[ForeignKey("Customerid")]
pubic virtual Customer customer {get; set;}
我越來越添加約束獲取的事務表客戶的詳細資料異常如下
其他資料:類型「Dutch.Models.Transaction」的ForeignKeyAttribute物業「客戶」 無效。在依賴類型 'Dutch.Models.Transaction'上找不到'外部關鍵字' 'Customerid'。名稱值應該是以逗號分隔的 外鍵屬性名稱列表。
在EF6中,據我所知,1對1關係將使用約定PK = FK,所以你有FK約束,但是ese會考慮Id的。如果您在交易中添加了客戶類型,只要您懶惰地/明確地加載它們,您就可以獲取詳細信息。 – DevilSuichiro
你能解釋一下,我仍然不能理解 – Pragadees
這可能有所幫助:http://stackoverflow.com/questions/15483019/entity-framework-code-first-how-to-annotate-a-foreign-key-for-a- default-valu –