Error trying to change a user’s role

Asked

Viewed 114 times

1

I am getting the following error while trying to change a user’s role :

Server Error in Application '/'.

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

get 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:             }

He says userid cannot be found but the userid he receives by parameter is valid (I checked and the same was in db), I do not know if where is my error, I am doing something wrong ? follows below 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 tried this too :

[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 mistake was another :

Server Error in Application '/'.

Validation failed for one or more entities. See 'Entityvalidationerrors' Property for more Details.

Description: An untreated exception occurred during the execution of current web request. Examine stack tracking to get more information about the error and where it originated in the code.

Details of the Exception: 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 missing in the construction of this method.

1 answer

1


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

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

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 how include and exclude types in the Entity Framework.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.