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

Asked

Viewed 922 times

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 mistake happens when it arrives on the following line: db.SaveChanges();, not to mention that he doesn’t even recognize this line: await db.SaveChangesAsync(); Follows 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 any idea how I can solve this problem? Thank you!

  • Could update the question with the message inside Entityvalidationerrors?

1 answer

1


This message indicates that there are entity validation errors. Add this snippet 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;
}
  • Hi! It gives throw error: An Exception of type 'System.Data.Entity.Validation.Dbentityvalidationexception' occurred in GTR_2.0.exe but was not handled in user code Additional information: Validation failed for one or more entities. See 'Entityvalidationerrors' Property for more Details.

  • I changed the Console by Debug.Writeline, change this in the code as well. Yes, it’s actually Throw that triggers the error. But before that, the block catch captured the error caused in db.SaveChanges() . Check in the output window as the lines System.Diagnostics.Debug.WriteLine will give the details of the error.

  • If the Output window is not appearing at the time of debug go to the menu View from visual studio and select the option output

  • It worked, now it shows the error: "Entity of type "cad_usuario" with state "Added" has the following validation errors: - Property: "Password_user", Error: "The Password_user field is required." " But it’s strange because I fill the textbox with the password. What might be?

  • Check at debug the value of the object addUsuario.cadUsuarioUsuarioInfo and its properties. The mandatory properties must be

  • I checked while debugging that all fields receive value, except the password. But I don’t know where I get it. There are other properties that must be checked?

  • What is the data type of this field? Try Manually force a value at debug time to see what the behavior is.

  • I switched to varchar to test, but I was char. I pushed during the debug and it was normally, no error, and saved in db.

  • If passing "at hand" worked, check again how your entity is being loaded, and if pertinent, the mapping (Mapping) of entities.

  • oh, it worked here, I started the 0 project and it worked. I think he’s crazy. or I changed something that I shouldn’t and crazy is me. Thank you so much!

Show 5 more comments

Browser other questions tagged

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