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(); 
}

Role模型类

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();
    }
}

 

你可能感兴趣的:(NetCore,EF,Linq,List,泛型)