'ASP.net Core: There is already an open DataReader associated with this Connection which must be closed first
I have an ASP.Net Core application, and I'm using Microsoft identity, I'm trying to add a page to manage users roles, in the action result i defined a variable which contain user's information and it's all good but whenever i add the roles into this variable this error occurred(There is already an open Data Reader associated with this Connection which must be closed first.)
The highlighted line is caused the error and without it, everything works fine.
controller:
using CTS_System6.Models;
using CTS_System6.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace CTS_System6.Controllers
{
[Authorize(Roles ="Admin")]
public class UsersController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public UsersController(UserManager<ApplicationUser> userManager, RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
public async Task<IActionResult> Index()
{
var users = await _userManager.Users.Select(user => new UserViewModel
{
Id = user.Id,
FirstName = user.FirstName,
LastName = user.LastName,
Email = user.Email,
//this is the line which cause the error message
***Roles = _userManager.GetRolesAsync(user).Result***
}).ToListAsync();
return View(users);
}
}
}
```[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/hkS8T.png
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|