Error: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details

0

The following error occurs when I try to insert data into the system: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details . It turns out that the error happens when it arrives at the following line: db.SaveChanges(); , not to mention that it does not even recognize this line: await db.SaveChangesAsync(); Here is the code:

private async void metroTile5_Click(object sender, EventArgs e)
        {
            using (frm_AddUsuario addUsuario = new frm_AddUsuario(new cad_usuario()))
            {
                if (addUsuario.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        cadusuarioBindingSource.Add(addUsuario.cadUsuarioUsuarioInfo);
                        db.cad_usuario.Add(addUsuario.cadUsuarioUsuarioInfo);
                       // await db.SaveChangesAsync();
                        db.SaveChanges();
                    }
                    catch(Exception ex)
                    {
                        MessageBox.Show(ex.Message, "Mensagem", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

        }

Does anyone have an idea how I can solve this problem? Thank you!

    
asked by anonymous 29.08.2017 / 23:51

1 answer

1

This message indicates that there are entity validation errors. Add this passage in the code to identify validation errors.

try
{
    // seu código...
    db.SaveChanges();
}
catch (DbEntityValidationException e)
{
    foreach (var eve in e.EntityValidationErrors)
    {
        System.Diagnostics.Debug.WriteLine("Entidade do tipo \"{0}\" com estado \"{1}\" tem os seguintes erros de validação:",
            eve.Entry.Entity.GetType().Name, eve.Entry.State);
        foreach (var ve in eve.ValidationErrors)
        {
            System.Diagnostics.Debug.WriteLine("- Propriedade: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage);
        }
    }
    throw;
}
    
30.08.2017 / 00:12