ASP.NET Identity 2 UserManager 异步获取所有用户 [英] ASP.NET Identity 2 UserManager get all users async

查看:33
本文介绍了ASP.NET Identity 2 UserManager 异步获取所有用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能告诉我是否有办法让 ASP.NET Identity 2 中的所有用户异步吗?

Can somebody tell if there is a way to get all users async in ASP.NET Identity 2?

UserManager.Users 中没有任何异步或找到所有异步或类似的东西

In the UserManager.Users there is nothing async or find all async or somwething like that

推荐答案

没有办法直接与 UserManager 类异步执行此操作.您可以将其包装在您自己的异步方法中:(这可能有点邪恶)

There is no way to do this asynchronously with the UserManager class directly. You can either wrap it in your own asynchronous method: (this might be a bit evil)

public async Task<IQueryable<User>> GetUsersAsync
{
    return await Task.Run(() =>
    {
        return userManager.Users(); 
    }
}

或者使用ToListAsync扩展方法:

public async Task<List<User>> GetUsersAsync()
{
    using (var context = new YourContext())
    {
        return await UserManager.Users.ToListAsync();
    }
}

或者直接使用您的上下文:

Or use your context directly:

public async Task<List<User>> GetUsersAsync()
{
    using (var context = new YourContext())
    {
        return await context.Users.ToListAsync();
    }
}

这篇关于ASP.NET Identity 2 UserManager 异步获取所有用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆