I have overridden my ApplicationDbContext.SaveChanges()
method. This helps me by providing immediately visible error messages on the yellow and white error application error screen.
But when there is a validation error with a DateTime
column, the catch clause doesn't seem to be activated.
Why is that? And how might I identify the invalid column?
Override method
public partial class ApplicationDbContext
{
public override int SaveChanges()
{
try
{
return base.SaveChanges(); //**error is thrown here**//
}
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
);
}
}
}
However the exception isn't thrown with DateTime
columns, it appears that the catch block is not being activated.