Error trying to change a user's role

1

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.

    
asked by anonymous 10.10.2017 / 10:24

1 answer

1

Your data entity has a property that is not correctly mapped by the Entity Framework. In the case of EntityValidationErrors .

I recommend directing the EF to ignore this property - since it is a state of the entity and not a value of it.

class MyContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Ignore<BlogMetadata>();
    }
}

public class Blog
{
    public int BlogId { get; set; }
    public string Url { get; set; }

    public BlogMetadata Metadata { get; set; }
}

public class BlogMetadata
{
    public DateTime LoadedFromDatabase { get; set; }
}

See more in this document on include and exclude types in the Entity Framework .

    
10.10.2017 / 12:55