在ASP核心标识核心中找不到要加入的表

时间:2018-11-26 11:49:35

标签: c# asp.net asp.net-core asp.net-identity asp.net-core-identity

我在aspcore的identitycore 2中有3个表,分别用于UsersRolesUserRole

Tabels Image

所以我需要用3个表的所有字段填充UserRoleViewModel

我将这段代码用于连接表格:

   public class ApplicationUserManager : UserManager<User>,
        IApplicationUserManager
    {
        private readonly IUnitOfWork _uow;
_passwordValidators;
        private readonly IServiceProvider _services;
        private readonly DbSet<User> _users;
        private readonly DbSet<Role> _roles;
        private readonly DbSet<UserRole> _userRoles;
        private readonly IApplicationUserStore _userStore;

            IUnitOfWork uow,
            IUsedPasswordsService usedPasswordsService)
            : base((UserStore<User, Role, ApplicationDbContexct, int, UserClaim, UserRole, UserLogin, UserToken, RoleClaim>)storer)
        {

            _users = uow.Set<User>();
            _roles = uow.Set<Role>();
            _userRoles = uow.Set<UserRole>();
        }

 public List<UserRoleViewModel> FindUserRole()
        {
            var userinfo=from users in Users 
                         join userRole in _userRoles on  users
        }

但是当我需要例如user.id时,它不会显示用户tabel的属性。所有表都有此问题。

我如何加入这些表格?

1 个答案:

答案 0 :(得分:0)

我使用此代码返回有关用户及其角色的所有信息:

    [HttpGet("UserList")]
    public IEnumerable<User> UserList()
    {
        return _applicationUserManager.Users.ToList();

    }
 [HttpGet("Roles")]
    public async Task<IActionResult> Role()
    {
        List<UserRoleViewModel> URVM = new List<UserRoleViewModel>();
        foreach (var item in UserList())
        {
            var users = await _applicationUserManager.FindUserById(item.Id);
            var roles = await _applicationUserManager.GetRolesAsync(users);
            var roleName = await _applicationRoleManager.FindRoleByNameList(roles[0]);
            URVM.Add(new UserRoleViewModel
            {
                Id = users.Id,
                Email = users.Email,
                BirthDate = users.BirthDate,
                CreatedDateTime = users.CreatedDateTime,
                FirstName = users.FirstName,
                IsActive = users.IsActive,
                PhoneNmuberConfirmed=users.PhoneNumberConfirmed,
                IsEmailPublic = users.IsEmailPublic,
                LastName = users.LastName,
                TwoFactorEnabled = users.TwoFactorEnabled,
                EmailConfirmed = users.EmailConfirmed,
                LockoutEnabled = users.LockoutEnabled,
                LastVisitDateTime = users.LastVisitDateTime,
                Location = users.Location,
                PhotoFileName = users.PhotoFileName,
                RoleLevel = roleName.RoleLevel,
                Description = roleName.Description,
                roleId = roleName.Id
            });
        }
        return Ok(URVM);
    }