I have 3 tables that need to be mapped with Entity Framework and I'm not sure the proper way to go about this. Here are my 3 entities:
public class User
{
[Key]
public int user_id {get; set;}
public string user_name {get; set;}
public virtual List<Role> roles {get; set;}
}
public class Role
{
[Key]
public int role_id {get; set;}
public string role_name {get; set;}
}
public class User_Role
{
[Key]
public int user_role_id {get; set;}
public int user_id {get; set;}
public int role_id {get; set;}
}
Please note that the User_Role entity just represents a lookup table to link many roles to a single user.
With SQL I would just do something like:
SELECT User.user_name, Role.role_name FROM User INNER JOIN User_Role ON User_Role.user_id = User.user_id INNER JOIN Role ON Role.role_id = User_Role.role_id WHERE User.user_id = 123
I am relatively new to Entity Framework so I'm not sure the best way to tackle this using EF4 DbContext (and possibly Fluent API?) but I'm hoping its pretty straight forward.
Thanks in advance.
It turns out I needed to use Fluent API to map a many to many table (User_Role).