2

次のようなデータベースがあります。

tblUsers
   - UserId
tblRoles
   - RoleId

tblUserRoles
   - UserRoleId
   - RoleId
   - UserId

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

   [ForeignKey("UserId")] // how does this tell ef not to map it to the primary key. 
                             It needs to map to UserId which is not defined as a key.??

   public DbSet<UserRole> Roles{ get; set; }
}    

class UserRoles
{
   [Key]
   public virtual int UserRoleId{ get; set; }

   public virtual int UserId{ get; set; }
   public virtual int RoleId{ get; set; }

   [ForeignKey("RoleId")]
   Public Role RoleInfo {get; set;}
}

class Role 
{
   [Key] 
   public virtual int RoleId {get; set;}

   public string RoleName {get; set;}
}

これは正しくないと思います。外部キーが主キーではなくテーブルの列であるモデルをセットアップする必要があります。私の質問: モデルを定義し、主キーなしでエンティティ間の関係をリンクするにはどうすればよいですか?

4

2 に答える 2