2017-06-21 11 views
0

"User"と "Product"エンティティがあり、製品には外部キーがあります(UserId - > User)。私のでC#エンティティフレームワークの外部キー属性が無視されました

 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"); 

:私は

[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; } 

私の更新された製品のエンティティ(例)

製品のエンティティでユーザーに1つの以上の外部キーを追加しようとしますが、間違った移行を得ることが

いAutomaticaly移行を作成しましたUserIdプロパティは無視されます

+0

元の移行はどのように見えるのですか(ここで変更されたもの) – grek40

+0

あなたはナビゲーションプロパティ( 'AcceptedBy'、' User')に 'ForeignKey'属性を必要としません –

+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

私はそれを試して、助けていない –

関連する問題