EFCoreマルチペアマルチコンフィギュレーション


Userモデルクラス
public class User
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int Age { get; set; }
    public bool Gender { get; set; }

    //          (    )
    //public virtual List UserRoleRelations { get; set; } = new List(); 
}


ロールモデルクラス
public class Role
{
    public int Id { get; set; }
    public string RoleName { get; set; }
    //          (    )
    //public virtual List UserRoleRelations { get; set; } = new List();
}

ユーザーロール関係モデル
public class UserRoleRelation
{
    public int Id { get; set; }
    public int UserId { get; set; }
    public int RoleId { get; set; }

    public virtual User User { get; set; }
    public virtual Role Role { get; set; }
}

モデル構成
using Microsoft.EntityFrameworkCore;

namespace MvcApp.Controllers
{
    public class MyDbContext : DbContext
    {
        public DbSet User { get; set; }
        public DbSet Role { get; set; }
        public DbSet UserRoleRelation { get; set; }
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            //  sql server   
            //optionsBuilder.UseSqlServer("Data Source=192.168.0.207,1433;Initial Catalog=db_hk_wx;User ID=hkuser;Password=zkhk2017")

            //  mysql    
            optionsBuilder.UseMySql("Data Source=192.168.10.27;port=3306;Initial Catalog=FBTest;user id=root;password=zkhk2014;Character Set=utf8");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //       
            var user = modelBuilder.Entity();
            user.ToTable("User");
            user.HasKey(r => r.Id);
            user.Property(r => r.Name).HasMaxLength(50).IsRequired();

            

            var role = modelBuilder.Entity();
            role.ToTable("Role");
            role.HasKey(r => r.Id);
            role.Property(r => r.RoleName).HasMaxLength(50).IsRequired();

            var userRoleRelation = modelBuilder.Entity();
            userRoleRelation.ToTable("UserRoleRelation");
            userRoleRelation.HasKey(r => r.Id);
            userRoleRelation.Property(r => r.UserId).IsRequired();
            userRoleRelation.Property(r => r.RoleId).IsRequired();

            //    ,      
            userRoleRelation.HasOne(r => r.User).WithMany().HasForeignKey(r => r.UserId).IsRequired();
            //    ,      
            userRoleRelation.HasOne(r => r.Role).WithMany().HasForeignKey(r => r.RoleId).IsRequired(); 

        }
    }
}

コントローラの使用
public class HomeController : Controller
{
    public IActionResult Index()
    {
        using (MyDbContext db = new MyDbContext())
        {
            var user = db.User.Where(r => r.Id == 1).First(); ;
            var userid = user.Id;
            //     Include     “ ”  ,   “ ”    
            var relations = db.UserRoleRelation.Include(r => r.Role).Where(r => r.UserId == userid).ToList();

            List roleNames = new List();
            foreach (var relation in relations)
            {
                roleNames.Add(relation.Role.RoleName);//           
            }
            

        };
        return View();
    }
}