EFCore 多对多配置

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Fanbin168/article/details/81745861

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<UserRoleRelation> UserRoleRelations { get; set; } = new List<UserRoleRelation>(); 
}

Role模型类

public class Role
{
    public int Id { get; set; }
    public string RoleName { get; set; }
    //一个角色对应多个关系(不要这个)
    //public virtual List<UserRoleRelation> UserRoleRelations { get; set; } = new List<UserRoleRelation>();
}

用户角色关系模型

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> User { get; set; }
        public DbSet<Role> Role { get; set; }
        public DbSet<UserRoleRelation> 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>();
            user.ToTable("User");
            user.HasKey(r => r.Id);
            user.Property(r => r.Name).HasMaxLength(50).IsRequired();

            

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

            var userRoleRelation = modelBuilder.Entity<UserRoleRelation>();
            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<string> roleNames = new List<string>();
            foreach (var relation in relations)
            {
                roleNames.Add(relation.Role.RoleName);//这里到的所有角色的名称
            }
            

        };
        return View();
    }
}

 

展开阅读全文

没有更多推荐了,返回首页