I’m very new at this, I’d really like some help with this problem:
First I already have a database, say I have 3 tables in this database: User, UserMapping, UserRole.
I generated a model with 3 class as follows:
public class user
{
public int UserID { get; set; }
public string UserName { get; set; }
public virtual ICollection<UserMapping> UserMappings { get; set; }
}
public class UserMapping
{
public int UserMappingID { get; set; }
public int UserID { get; set; }
public int UserRoleID { get; set; }
public virtual User User { get; set; }
public virtual UserRole UserRole { get; set; }
}
public class UserRole
{
public int UserRoleID { get; set; }
public string RoleName { get; set; }
public virtual ICollection<UserMapping> UserMappings { get; set; }
}
I want to create a new user, and add a User Role for the new user. User and User Role have no direct relationship. I want to make a view to create a new user, using the User model class with strongly-typed. How can I make a User Role for the user in this view?
Please help…! Thank you very much in advance.
You don’t need to create UserMapping entity by your self . Because when you specify your many to many relationship between User and UserRole EF will automatically create the relationship table
UserUserRolerelationship table for you.Then you can map the relationship table to the existing one,