I am working on a console app for daily imports into our CMS from a parent ERP system. I am having a funky issue with try/catch blocks. Errors are happening inside my try's and not making it to the catch. At first, it did go into the catch and I was able to figure out the issue, but now it's just erroring on the try and staying there. Hitting continue doesn't work, just keeps showing the error again and again. The error has to do with EF and one of the models not having a required field...
public void Save()
{
try
{
_context.SaveChanges(); <--bombs here and never lets me step thru
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.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)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
}
It is probably throwing more or a different exception. Try using
catch (Exception e)
And see if it now enters the catch and log your exception to know exactly what you are getting