【问题标题】:complex query for many to many relatinoship in ef 3ef 3中多对多关系的复杂查询
【发布时间】:2021-04-07 08:36:37
【问题描述】:

在我的项目中,每个用户都有一个或多个组,每个组都有一个或多个角色 在用户登录后,我想使用实体框架 3 获取该用户的角色。 这些是我拥有的模型。

用户模型

public class User
    {

        public User()
        {
            userGroups = new List<UserGroup>();
        }
        public int Id { get; set; }

        [Display(Name = "UserName")]
        [Required]
        public string Username{get;set;}

        public ICollection<UserGroup> userGroups{get;set;}

        
    }

组模型

public class Group
    {
        public int Id { get; set; }

        [Display(Name = "groupName")]
        [Required]
        public string groupName { get; set; }

        public ICollection<RoleGroup> roleGroups { get; set; }
        public ICollection<UserGroup> userGroups { get; set; }

    }

用户组模型

public class UserGroup
    {
        public int userId{get;set;}
        public User user{get;set;}
        public int groupId{get;set;}
        public Group group{get;set;}
    }

榜样

public class Role
    {
        public int Id{get;set;}

        public string roleName{get;set;}

        public string roleDescription{get;set;}
        public ICollection<RoleGroup> roleGroups{get;set;}
    }

角色组模型


 public class RoleGroup
    {
        public int RoleId{get;set;}
        public Role role{get;set;}
        public int groupId{get;set;}
        public Group group{get;set;}
    }

我想要的是使用 ID 获取特定用户的所有角色。

提前致谢。

【问题讨论】:

    标签: .net asp.net-mvc entity-framework asp.net-core .net-core


    【解决方案1】:

    您可以看到从用户到角色的路径相当清晰:

    用户 -> 用户组 组 -> 角色组 角色。

    所以不是很复杂,查询可以是这样的:

    //with lazy loading enabled
    var userRoles = context.Users.FirstOrDefault(e => e.Id == someId)
                          ?.userGroups
                          ?.SelectMany(e => e.group.roleGroups)
                          ?.Select(e => e.role)?.ToList();
    
    //with lazy loading disabled
    var userRoles = context.Users.Where(e => e.Id == someId)
                           .SelectMany(e => e.userGroups)
                           .SelectMany(e => e.group.roleGroups)
                           .Select(e => e.role).ToList();
    

    【讨论】:

      【解决方案2】:

      由于用户、组和角色表包含​​多对多关系,您可以使用IncludeThenInclude 方法加载相关数据。检查以下查询语句:

              //required using Microsoft.EntityFrameworkCore;
              var result = (_context.User
                  .Include(c => c.userGroups)
                  .ThenInclude(c => c.group)
                  .ThenInclude(c => c.roleGroups)
                  .ThenInclude(c => c.role)
                  .Where(c=>c.Id ==1)
                  .SelectMany(c => c.userGroups.SelectMany(d => d.group.roleGroups
                          .Select(e => new { UserName = c.Username, 
                                  GroupName = e.group.groupName,  
                                  RoleName = e.role.roleName })))).ToList();
      

      数据表如下(我们可以看到用户“Tom”有两个角色:“Admin”和“Manager”):

      查询结果如下:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-21
        • 1970-01-01
        • 2017-11-13
        相关资源
        最近更新 更多