Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. Code First

20,660

Solution 1

I don't know why writing to the VS output window doesn't work and how to make it work. But as a last resort just write the errors into a text file which should work independent of the type of application you have:

try
{
    context.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException e)
{
    var outputLines = new List<string>();
    foreach (var eve in e.EntityValidationErrors)
    {
        outputLines.Add(string.Format(
            "{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:",
            DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State));
        foreach (var ve in eve.ValidationErrors)
        {
            outputLines.Add(string.Format(
                "- Property: \"{0}\", Error: \"{1}\"",
                ve.PropertyName, ve.ErrorMessage));
        }
    }
    //Write to file
    System.IO.File.AppendAllLines(@"c:\temp\errors.txt", outputLines);
    throw;

    // Showing it on screen
    throw new Exception( string.Join(",", outputLines.ToArray()));

}

Solution 2

You can pass it up the exception stack like below.

try
{
    _dbContext.SaveChanges();
}
catch (DbEntityValidationException dbValEx)
{
   var outputLines = new StringBuilder();
   foreach (var eve in dbValEx.EntityValidationErrors)
   {
     outputLines.AppendFormat("{0}: Entity of type \"{1}\" in state \"{2}\" has the following validation errors:"
       ,DateTime.Now, eve.Entry.Entity.GetType().Name, eve.Entry.State);

     foreach (var ve in eve.ValidationErrors)
     {
       outputLines.AppendFormat("- Property: \"{0}\", Error: \"{1}\""
        ,ve.PropertyName, ve.ErrorMessage);
     }
   }

 throw new DbEntityValidationException(string.Format("Validation errors\r\n{0}"
  ,outputLines.ToString()), dbValEx);
}
Share:
20,660
Yustme
Author by

Yustme

Updated on July 09, 2022

Comments

  • Yustme
    Yustme almost 2 years

    I get this error:

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

    when i try to update the database with the Update-Database command in the Package Manager Console.

    How can I write the lines to the output window in visual studio?

    I tried:

    try
    {
        context.SaveChanges();
    }
    catch (System.Data.Entity.Validation.DbEntityValidationException e)
    {
        foreach (var eve in e.EntityValidationErrors)
        {
            System.Diagnostics.Debug.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                eve.Entry.Entity.GetType().Name, eve.Entry.State);
            foreach (var ve in eve.ValidationErrors)
            {
                System.Diagnostics.Debug.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
                    ve.PropertyName, ve.ErrorMessage);
            }
        }
        throw;
    }
    

    But that didn't work. Any other suggestions on how to debug this?

  • user2394196
    user2394196 almost 11 years
    @Slauma: Thank you.. Nice trick.. Never thought about that.. This is where the difference figures out between good developers and very good ones! ;)
  • Yustme
    Yustme over 10 years
    Thanks, i'll give this a try too in my next project! +1