2017-06-21 66 views
0

我有「用戶」和「產品」實體,產品有外鍵給用戶(UserId - > User)。我想多一個外鍵添加到用戶的產品實體,但得到錯誤的遷移C#實體框架外鍵屬性被忽略

我的更新產品實體(例如)

[ForeignKey("User")] 
    public int? UserId { get; set; } 
    [ForeignKey("AcceptedBy")] 
    public int? AcceptedById { get; set; }  
    [ForeignKey("AcceptedById")] 
    public User AcceptedBy { get; set; } 
    [ForeignKey("UserId")] 
    public User User { get; set; } 

Automaticaly創建遷移:

 DropForeignKey("dbo.Products", "UserId", "dbo.Users"); 
     AddColumn("dbo.Products", "AcceptedById", c => c.Int()); 
     AddColumn("dbo.Products", "User_Id", c => c.Int()); 
     CreateIndex("dbo.Products", "AcceptedById"); 
     CreateIndex("dbo.Products", "User_Id"); 
     AddForeignKey("dbo.Products", "AcceptedById", "dbo.Users", "Id"); 
     AddForeignKey("dbo.Products", "User_Id", "dbo.Users", "Id"); 
     DropColumn("dbo.Products", "AcceptedByUserId"); 

所以我UserId屬性被忽略

+0

最初的遷移是怎麼樣的(在這裏更改的)? – grek40

+0

你不需要導航屬性上的'ForeignKey'屬性('AcceptedBy','User') –

+0

@ Mohamed Ahmed我試過了 - 沒有幫助 –

回答

0

我認爲你有很多ForeignKey數據註釋。

public int? UserId { get; set; } 
[ForeignKey("UserId")] 
public User User { get; set; } 

public int? AcceptedById { get; set; }  
[ForeignKey("AcceptedById")] 
public User AcceptedBy { get; set; } 
+0

我試過了,沒有幫助 –