首页 > 代码库 > 捕捉Entity framework 6的详细异常提示

捕捉Entity framework 6的详细异常提示

采用

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         }

 链接: http://stackoverflow.com/questions/15820505/dbentityvalidationexception-how-can-i-easily-tell-what-caused-the-error

捕捉Entity framework 6的详细异常提示