EF Code First: How do I see 'EntityValidationErrors' property from the nuget package console?

jeremy picture jeremy · Apr 19, 2012 · Viewed 71.1k times · Source

I'm at a loss for this:

I've defined my classes for a entity framework (4.1.3) code first approach. Everything was fine (I was creating the tables etc.) until I started to Seed.

Now when I do the

Add-Migration "remigrate" ; Update-Database;

I get an error on the package console "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."

I have a breakpoint in my Seed() method but because I'm running this on the console when the project is not running, I'm clueless as to how to get to the details (PS - I've seen the thread Validation failed for one or more entities while saving changes to SQL Server Database using Entity Framework which shows how I can see the property.)

I know that my Seed() method has a problem because if I put a return right after the method call, the error goes away. So how do I set my breakpoint so I can see what the validation error is? Kinda lost. Or is there some other way to trace it in the nuget console??

Answer

Richard picture Richard · May 20, 2012

I got annoyed by this recently too. I fixed it by putting a wrapper function in the Configuration class in the Seed method, and replaced calls to SaveChanges with calls to my function instead. This function would simply enumerate the errors within the EntityValidationErrors collection, and rethrow an exception where the Exception message lists the individual problems. This makes the output show up in the NuGet package manager console.

Code follows:

/// <summary>
/// Wrapper for SaveChanges adding the Validation Messages to the generated exception
/// </summary>
/// <param name="context">The context.</param>
private void SaveChanges(DbContext context) {
    try {
        context.SaveChanges();
    } catch (DbEntityValidationException ex) {
        StringBuilder sb = new StringBuilder();

        foreach (var failure in ex.EntityValidationErrors) {
            sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
            foreach (var error in failure.ValidationErrors) {
                sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                sb.AppendLine();
            }
        }

        throw new DbEntityValidationException(
            "Entity Validation Failed - errors follow:\n" + 
            sb.ToString(), ex
        ); // Add the original exception as the innerException
    }
}

Just replace calls to context.SaveChanges() with SaveChanges(context) in your seed method.