简体   繁体   English

我在使用 UserManager.IsInRoleAsync() 时收到 InvalidOperationException

[英]Im getting InvalidOperationException when using UserManager.IsInRoleAsync()

Im trying to get all users in the role that I'm editing.我试图让所有用户都担任我正在编辑的角色。 But when I load the page it keeps giving me但是当我加载页面时它一直给我

InvalidOperationException: Cannot set MySqlCommand.CommandText when there is an open DataReader for this command; InvalidOperationException:当此命令有打开的 DataReader 时,无法设置 MySqlCommand.CommandText; it must be closed first.它必须先关闭。

public async Task<IActionResult> EditRole(string id)
{
    var role = await roleManager.FindByIdAsync(id);
    if(role == null)
     {
        ViewBag.ErrorMessage = $"Role with id = {id} cannot be found";
        return View("NotFound");
    }
    var model = new EditRoleModel
    {
        Id = role.Id,
        RoleName = role.Name
    };
    foreach(var user in userManager.Users)
    {
        var userInRole = await userManager.IsInRoleAsync(user, role.Name);
        if (userInRole)
        {
            model.Users.Add(user.UserName);
        }
    }
    return View(model);
}

I'm getting the error here我在这里收到错误

var userInRole = await userManager.IsInRoleAsync(user, role.Name);

The Users property is an IQueryable<User> , and it looks like it is keeping an open streaming query over the data while you iterate it, which is causing the problem if you try and execute a second operation. Users属性是一个IQueryable<User>看起来它在您迭代数据时保持对数据的开放流式查询,如果您尝试执行第二个操作,这会导致问题。

As such, assuming the user directory isn't huge , you can probably work around this by simply:因此,假设用户目录不是很大,您可以简单地解决这个问题:

foreach(var user in userManager.Users.ToList())
{...}

which completes thew first query eagerly before iterating.它在迭代之前急切地完成了第一个查询。


However!然而! You probably want to look at GetUsersInRoleAsync instead :你可能想在看GetUsersInRoleAsync代替

foreach (var user in userManager.GetUsersInRoleAsync(role.Name))
{
    model.Users.Add(user.UserName);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 _userManager.IsInRoleAsync(user, roleName) 只返回 false - _userManager.IsInRoleAsync(user, roleName) just return false 为什么我总是得到InvalidOperationException? - Why im getting all the time InvalidOperationException? 管理员声明已添加到IdentityUser中,但usermanager不能通过IsInRoleAsync找到它 - Admin claim added to IdentityUser, but the usermanager does not find it with IsInRoleAsync 我不时得到异常InvalidOperationException:如何修复它? - From time to time im getting exception InvalidOperationException: how can fix it? 使用具体类作为Controller属性.NET Core时获取InvalidOperationException - Getting an InvalidOperationException when using a concrete class as a Controller property .NET Core 为什么在使用backgroundworker时出现InvalidOperationException异常? - Why i'm getting exception InvalidOperationException when using backgroundworker? 使用线程时发生InvalidOperationException - InvalidOperationException when using threading 使用 HotChocolate 从 UserManager 获取用户 - Getting users from UserManager using HotChocolate 使用 openTk GameWindow 扩展时出现错误 - Im getting an error when using the openTk GameWindow extention 使用AspNet.Identity UserManager时检测更改 - DetectChanges when using AspNet.Identity UserManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM