一、查询关联表数据

            StudyAboard_TestContext _context = new StudyAboard_TestContext();
            CrmRole role = _context.CrmRole
                .Include(q => q.CrmRoleMenu)
                .Where(q => q.Id == 1).FirstOrDefault();

 

二、清空关联表数据

            StudyAboard_TestContext _context = new StudyAboard_TestContext();
            CrmRole role = _context.CrmRole
                .Include(q => q.CrmRoleMenu)
                .Where(q => q.Id == 1).FirstOrDefault();

            //清空关联表数据
            _context.CrmRoleMenu.RemoveRange(role.CrmRoleMenu);
            _context.SaveChanges();

 

三、添加关联表数据

1.完全添加

            CrmRole role = new CrmRole()
            {
                Name = "测试角色"
            };

            role.CrmRoleMenu.Add(new CrmRoleMenu()
            {
                MenuId = 1
            });
            role.CrmRoleMenu.Add(new CrmRoleMenu()
            {
                MenuId = 2
            });
            _context.CrmRole.Add(role);
            _context.SaveChanges();

 

2.读取后添加

            StudyAboard_TestContext _context = new StudyAboard_TestContext();
            CrmRole role = _context.CrmRole
                .Include(q => q.CrmRoleMenu)
                .Where(q => q.Id == 1).FirstOrDefault();

            //添加关联表数据
            role.CrmRoleMenu.Add(new CrmRoleMenu()
            {
                MenuId = 1
            });
            role.CrmRoleMenu.Add(new CrmRoleMenu()
            {
                MenuId = 2
            });
            _context.SaveChanges();

 

 

更多:

EF Core中执行Sql语句查询操作之FromSql,ExecuteSqlCommand,SqlQuery

.NetCore中EFCore的使用整理(二)-关联表查询

.NetCore中EFCore的使用整理

相关文章:

  • 2021-05-21
  • 2021-09-06
  • 2021-06-11
  • 2022-12-23
  • 2021-06-06
  • 2021-10-28
猜你喜欢
  • 2021-11-13
  • 2022-02-05
  • 2021-10-16
  • 2022-01-11
  • 2021-12-22
相关资源
相似解决方案