Question

I Have the following tables

Users
-> UserId
-> Name

Roles
-> RoleId
-> Name

UserRoles
-> UserId
-> RoleId

and the following classes

public class Role{
     public int RoleId{get;set;}
     public int Name{get;set;}
}

public class User{
     public int UserId{get;set;}
     public int Name{get;set;}
     public ICollection<Role> Roles{get;set;}
}

How to map this using EntityFramework ModelBinder.

Was it helpful?

Solution

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<User>()
        .HasMany(u => u.Roles)
        .WithMany()
        .Map(a => {
            a.ToTable("UserRoles");
            a.MapLeftKey("UserId");
            a.MapRightKey("RoleId");
        });
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top