Skip to content

Instantly share code, notes, and snippets.

@FabianoCampos
Last active February 11, 2020 11:36
Show Gist options
  • Save FabianoCampos/06a61a57271f9a7b476ba3aa8c87b02e to your computer and use it in GitHub Desktop.
Save FabianoCampos/06a61a57271f9a7b476ba3aa8c87b02e to your computer and use it in GitHub Desktop.
Para quando houver erro: Validation failed for one or more entities. See 'EntityValidationErrors' property for more details
/**
Para quando houver erro:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details.
*/
try
{
contexto.SaveChanges();
}
catch (DbEntityValidationException e)
{
/**
var raise = (from validationErrors in dbEx.EntityValidationErrors from validationError in validationErrors.ValidationErrors select string.Format("{0}:{1}", validationErrors.Entry.Entity, validationError.ErrorMessage))
.Aggregate<string, Exception>(dbEx, (current, message) => new InvalidOperationException(message, current));
**/
var msg = "";
foreach (var eve in e.EntityValidationErrors)
{
msg += string.Format("Entidade do tipo \"{0}\" no estado \"{1}\" tem os seguintes erros de validação:", eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
msg += string.Format("- Property: \"{0}\", Erro: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
}
}
throw new Exception(msg);
}
catch (Exception ex)
{
throw ex;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment