2013-08-18 62 views
1

任何人都可以幫助我嗎?EF代碼 - 首先創建一些字段,即使我添加了ForeignKey註釋

這裏是我的2班

class Request 
{ 
    public Nullable<int> BuyCurrencyId {get ; set;} 
    public Nullable<int> SaleCurrencyId {get ; set;} 

    [ForeignKey("SaleCurrencyId")] 
    public virtual Currency SaleCurrency { get; set; } 

    [ForeignKey("BuyCurrencyId")] 
    public virtual Currency BuyCurrency { get; set; } 
} 


class Currency 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 

    public virtual ICollection<Request> Requests { get; set; } 
    public virtual ICollection<Request> Requests1 { get; set; } 
} 

我查了更新與EF數據庫,我發現了EF創建Reqyests表是這樣的:

SaleCurrencyId int (Already exists) 
BuyCurrencyId int (Already exists) 
Currency_Id int (Added by EF) 
Currency_Id1 int (Added by EF) 

通過這不是事情我期望。我最後的兩列不正確,它們不存在。

任何人都可以幫助我嗎?

我使用EF 6個字母與T4.Please我生成的模型來更新現有的數據庫牢記這一點,我想用數據註解,不流利的API

對不起我的壞英語

更新1

我想如果我改變貨幣類這將解決我的問題,但事實並非如此。

class Currency 
{ 
    public int Id { get; set; } 
    public string Name { get; set; } 

    [InverseProperty("SaleCurrencyId")] 
    public virtual ICollection<Request> Requests { get; set; } 
    [InverseProperty("BuyCurrencyId")] 
    public virtual ICollection<Request> Requests1 { get; set; } 
} 

回答

2

你UPDATE1幾乎是正確的解決方案,但[InverseProperty]屬性的參數必須是導航屬性在Request,不是國外的主要特性:

[InverseProperty("SaleCurrency")] 
public virtual ICollection<Request> Requests { get; set; } 

[InverseProperty("BuyCurrency")] 
public virtual ICollection<Request> Requests1 { get; set; }