سؤال

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.

هل كانت مفيدة؟

المحلول

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");
        });
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top