I have a db that looks like this:
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;}
}
I don't think this is correct. I need to setup the model where the foreign key is not the primary key but a column in the table. My question: How do I define my model and link the relationship between entities without having a primary key?