我有一个看起来像这样的数据库:
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;}
}
我不认为这是正确的。我需要设置外键不是主键而是表中的列的模型。我的问题:如何在没有主键的情况下定义我的模型并链接实体之间的关系?