2016-10-22 4 views
0

エンティティフレームワークのコードを最初に使用して1-n関係を作成しようとしています。私は何を意味するユーザーIDは、投票表の外部キーでなければならないことである以下の私のクラスは外部キーを作成しない最初のアプローチ

public class User 
{ 

    [Key] 
    public int UserID { get; set; } 


    //public virtual Vote Vote { get; set; } 
    [Required] 
    public string Name { get; set; } 
    [Required] 
    public string Email { get; set; } 
    [Required] 
    public string Bio { get; set; } 
    [Required] 
    public string Education { get; set; } 
    [Required] 
    public string Experience { get; set; } 

    public string Password { get; set; } 

} 

であり、次は私の場合には、私の第二のクラス

public class Vote 
{ 
    [Required] 
    [Key] 
    public int VoteID { get; set; } 

    [ForeignKey("UserID")] 
    [Required] 
    public virtual User User { get; set; } 

    //public virtual int UserID { get; set; } 

    [Required] 
    public int VoteCount { get; set; } 
    [Required] 

    public int UserID { get; set; } 


} 

ユーザーと投票して1-nの関係であります。私は、次のコマンドを使用してDBの作成が完了するとき、私はUserID

任意の外部キーまたは列名を取得していないです

enable-migrations -ContextTypeName ProfileOne.PO -Force 

    Add-migration PO 

    update-database 

私は結果を達成することはできませんよしかし、なぜすべてのヘルプは理解されるであろう。

+1

これは 'UserID'で' Vote'テーブルを作成するはずです。しかし、この関係は1-1ではなく、1-n(ユーザーは投票数が多い)です。 'UserID'が作成されていなければ、私たちは見ることのできないコードで何か間違っています。 –

+0

このシナリオでは、EFは1対1の関係を期待しています。 EFのバージョンによっては、このような関係で慣習でPKをFKとして使用するため、属性は無視されることがあります。 – DevilSuichiro

+0

外部キーが作成されるはずです。ここではパズルの一部が欠落しています。この種の問題のために[MCVE]を作成するのに十分なほど簡単です。 –

答えて

1

は、次のアプローチを使用してください:詳細については

public class User 
{ 
    [Key] 
    public int UserID { get; set; } 

    [Required] 
    public string Name { get; set; } 

    [Required] 
    public string Email { get; set; } 

    [Required] 
    public string Bio { get; set; } 

    [Required] 
    public string Education { get; set; } 

    [Required] 
    public string Experience { get; set; } 

    public string Password { get; set; }   


    //Navigation properties 
    public virtual ICollection<Vote> Votes { get; set; } 
} 


public class Vote 
{ 
    [Key] 
    public int VoteID { get; set; } 

    [Required] 
    public int VoteCount { get; set; } 


    //Foreign key for User 
    [Required] 
    public int UserID { get; set; } 

    //Navigation properties 
    public virtual User User { get; set; } 
} 


を、Entity Relationshipsを見てください。これが役に立ったら...

-1

のように見えるように、あなたしているクラスを変更します。

public class User 
{ 
    [Key] 
    public int UserID { get; set; } 

    [Required] 
    public string Name { get; set; } 

    [Required] 
    public string Email { get; set; } 

    [Required] 
    public string Bio { get; set; } 

    [Required] 
    public string Education { get; set; } 

    [Required] 
    public string Experience { get; set; } 

    public string Password { get; set; } 

    public virtual Vote Vote { get; set; } 
} 

public class Vote 
{ 
    [Required] 
    [Key] 
    public int VoteID { get; set; } 

    [Required] 
    public int VoteCount { get; set; } 

    [Required] 
    public int UserID { get; set; } 

    //[ForeignKey("UserID")] 
    //[Required] 
    public virtual User User { get; set; } 
} 

2つのエンティティ間の関係はuがそれをコメントした仮想プロパティによって定義されます。

+1

いいえ、属性は重複しています(命名規則は同じことを行います)が、間違いではありません。 'virtual'修飾子も必要ありません。 –

関連する問題