EF Code First: How do I see 'EntityValidationErrors' property from the nuget package console?
Solution 1:
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.
Solution 2:
Extend Your DBContext Class Already With a Partial Class Definition!
If you look at the class definition for your DbContext it will be something like the following:
// DatabaseContext.cs -- This file is auto generated and thus shouldn't be changed.
public partial class [DatabaseContextName] : DbContext { ... }
So, in another file you can create the same definition and override the parts you want to.
// partialDatabaseContext.cs -- you can safely make changes
// that will not be overwritten in here.
public partial class [DatabaseContextName] : DbContext { // Override defaults here }
The whole idea with partial classes --did you notice the DbContext is a partial class-- is that you can extend a class that has been generated (or organize classes into multiple files) and in our case we also want to override the SaveChanges method from within a partial class that adds to the DbContext.
This way we can get error debugging information from all existing DbContext/SaveChanges calls everywhere and not have to change your Seed code or development code at all.
This is what I would do (NOTE the difference is that I just override SaveChanges method in the our own authored DbContext partial class, NOT THE GENERATED ONE). Also, make sure you partial class uses the correct namespace or you will be banging your head against the wall.
public partial class Database : DbContext
{
public override int SaveChanges()
{
try
{
return base.SaveChanges();
}
catch (DbEntityValidationException ex)
{
var 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
}
}
}