1

如何UserManagerMicrosoft.AspNetCore.Identity使用中获取用户列表[ScopedService]

以下是我已经尝试过的:

using System.Linq;
using hostapp.Data;
using hostapp.Models;
using HotChocolate;
using HotChocolate.Data;
using Microsoft.AspNetCore.Identity;

namespace hostapp.GraphQL
{
    public class Query
    {
        // 1.
        [UseDbContext(typeof(DataContext))]
        public IQueryable<AppUser> Users([ScopedService] UserManager<AppUser> userManager)
        {
            return (IQueryable<AppUser>)userManager.Users;
        }
        
        // 2.
        [UseDbContext(typeof(DataContext))]
        public async Task<List<AppUser>> Users([ScopedService] UserManager<AppUser> userManager)
        {
            return await userManager.Users.ToListAsync();
        }

    }
}

输入:

query {
  users {
    emailConfirmed
  }
}

输出:

{
  "errors": [
    {
      "message": "Unexpected Execution Error",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "users"
      ]
    }
  ],
  "data": {
    "users": null
  }
}
4

1 回答 1

1

你不需要使用[ScopedService],而是[Service] 你真的只需要在 a与 结合使用[ScopedService]的情况下使用。我们将在下一个版本中修复这种混淆DBContextUseDbContext

于 2021-04-16T16:34:09.837 回答