I'm getting the following error when trying to change the role of a user:
Application Server Error '/'.
UserId não encontrada. Descrição: Ocorreu uma exceção sem tratamento durante a execução da atual solicitação da Web. Examine o rastreamento de pilha para
More information about the error and where it originated in the code.
Detalhes da Exceção: System.InvalidOperationException: UserId não encontrada. Erro de Origem: Linha 91: else Linha 92: { Linha 93: await UserManager.AddToRoleAsync(aspnetuser.Id, "Ong"); Linha 94: return RedirectToAction("Index", "Home"); Linha 95: }
It says that the userId can not be found but the userId it receives per parameter is valid (I checked it and it was in db), I do not know if where is my error, am I doing something wrong? below is the method used for this:
[HttpGet]
public async Task<ActionResult> AprovarAsync(string Id)
{
IdentityRole aspnetuser = new IdentityRole();
if (Id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
else
{
await UserManager.AddToRoleAsync(aspnetuser.Id, "Ong");
return RedirectToAction("Index", "Home");
}
}
I've also tried the following:
[HttpGet]
public async Task<ActionResult> AprovarAsync(string Id)
{
var user = new ApplicationUser{ Id = Id };
if (Id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
else
{
await UserManager.AddToRoleAsync(user.Id, "Ong");
return RedirectToAction("Index", "Home");
}
}
But the error was another:
Application Server Error '/'.
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
Description: An unhandled exception occurred during the execution of the current Web request. Examine the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.Entity.Validation.DbEntityValidationException: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
I would like to know where I am going wrong in building this method.