捕捉Entity framework 6的詳細異常提示

採用spa

try
            {

            }
            catch (Exception e)
            {
                
                throw;
            }

 不能捕捉到詳細異常提示, e.message的內容爲"Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."code

若是須要獲取詳細的異常提示,採用blog

 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-errorget

相關文章
相關標籤/搜索