采用
try { } catch (Exception e) { throw; }
不能捕捉到详细异常提示, e.message的内容为"Validation failed for one or more entities. See ‘EntityValidationErrors‘ property for more details."
如果需要获取详细的异常提示,采用
1 try 2 { 3 return base.SaveChanges(); 4 } 5 catch (DbEntityValidationException ex) 6 { 7 var errorMessages = ex.EntityValidationErrors 8 .SelectMany(x => x.ValidationErrors) 9 .Select(x => x.ErrorMessage); 10 11 var fullErrorMessage = string.Join("; ", errorMessages); 12 13 var exceptionMessage = string.Concat(ex.Message, " The validation errors are: ", fullErrorMessage); 14 15 throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors); 16 }
时间: 2024-10-09 10:22:23