I have to following many-to-many model structure:
public class User
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid UserId { get; set; }
[Required]
[MaxLength(50)]
public string Name{ get; set; }
[Required]
[MaxLength(50)]
public string Email { get; set; }
public virtual ICollection<UserInRole> UserInRoles { get; set; }
}
public class Role
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int RoleId { get; set; }
public string RoleName { get; set; }
public virtual ICollection<UserInRole> UserInRoles { get; set; }
}
public class UserInRole
{
[Required]
public Guid UserId { get; set; }
[Required]
public int RoleId { get; set; }
public virtual User User { get; set; }
public virtual Role Role { get; set; }
}
ApplicationDbContext:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
// UserInRole: set both fields as primary
modelBuilder.Entity<UserInRole>().HasKey(table => new{ table.RoleId, table.UserId});
modelBuilder.Entity<User>()
.HasMany(c => c.UserInRoles);
modelBuilder.Entity<Role>()
.HasMany(c => c.UserInRoles);
The database is successfully created with the right relationships (many to many) but when I create a new Controller & View using scaffolding - the system generates the CRUD pages without the option to select the role for each user. is it normal? do I need to do this part by myself?