I am new to asp.net mvc and in the root code of entity code, I am not interested in databases either. I apologize in advance for the wrong terminology or the way I understand things.
Now to the question. I have the following models:
User model
public class User { public int UserId { get; set; } public string Username { get; set; } public string Password { get; set; } public int RoleId { get; set; } [ForeignKey("RoleId")] public virtual IEnumerable<Role> Roles { get; set; } }
Role model
public class Role { public int RoleId { get; set; } public string RoleName { get; set; } }
Ultimately, I want to use the Ef codefirst method with a white API to map UserId and RoleId to a User_Role table with a one-to-many relationship, the user can have several roles: 
I assume that what has been done on this issue is the correct approach, except that the author has used the many-to-many combination. I tried it this way, but the part with u => u.users gives me an error (I assume it is because the model does not have a user property, so he answered his question, but did not update his question?)
My question is: What is the exact free api code allowing Ef to generate this table for me?
Things I'm Not Sure About: (Feel free to ignore)
- Is this the right approach to my problem?
- Once I have a lookup table, is this still the right way to declare my navigation property so that I can later use it as user.Roles and restore their roles?
- where will the RoleId value be displayed in the User model from the role table or User_Role?
- Should I use an identifier in the lookup table?
Thanks in advance! I really appreciate your experience.
database asp.net-mvc-4 ef-code-first fluent
edgarpetrauskas
source share