2016-12-02 114 views
0
public class UserCommentry 
{ 
    [Key] 
    public int ID { get; set; } 
    [ForeignKey("AccountID")] 
    public int AccountID { get; set; } 
    public virtual Account account { get; set; } 
    public Int64 TransferID { get; set; } 
} 

主鍵字段如何處理外鍵關係實體框架MVC

public class Account 
{ 
    [Key] 
    public int ID { get; set; } 
    public int code{ get; set; } 
    public Int64 TransferID { get; set; } 

錯誤:

The property 'AccountID' cannot be configured as a navigation property. The property must be a valid entity type and the property should have a non-abstract getter and setter. For collection properties the type must implement ICollection where T is a valid entity type.

+1

你已經把'ForeignKey'屬性放在錯誤的地方。只需將其移至「帳戶」屬性的正上方即可。 –

回答

0

簡短的回答:那是因爲navigarion屬性類型應該爲你想要的它指向。 如果我想創建與帳戶的關係,我會做這種方式:

public class UserCommentry 
    { 
     [Key] 
     public int ID { get; set; } 
     public Account Account{ get; set; } 
     public virtual Account account { get; set; } 

     public Int64 TransferID { get; set; } 
    } 

然後讓上下文知道它使用OnModelCreating。你可以在這裏閱讀更多http://www.entityframeworktutorial.net/code-first/configure-one-to-many-relationship-in-code-first.aspx