2015-11-18 61 views
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'。名稱值應該是以逗號分隔的 外鍵屬性名稱列表。

+1

在EF6中,據我所知,1對1關係將使用約定PK = FK,所以你有FK約束,但是ese會考慮Id的。如果您在交易中添加了客戶類型,只要您懶惰地/明確地加載它們,您就可以獲取詳細信息。 – DevilSuichiro

+0

你能解釋一下,我仍然不能理解 – Pragadees

+0

這可能有所幫助:http://stackoverflow.com/questions/15483019/entity-framework-code-first-how-to-annotate-a-foreign-key-for-a- default-valu –

回答

0

我終於做到由外鍵約束 我的模型看起來像現在這樣

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; } 
    public ICollection<Transaction> Transactions { get; set; } 
} 

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 ICollection<Transaction> Transactions { 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; } 
[ForeignKey("Customerid")] 
public Customer customer {get; set;} 
[ForeignKey("Merchantid")] 
public Merchant merchant {get; set;} 
}