C# EF entity更新异常捕捉

 1  try
 2 {
 3 ......
 4 }
 5             catch (DbEntityValidationException exc)
 6             {
 7                 // just to ease debugging
 8                 foreach (var error in exc.EntityValidationErrors)
 9                 {
10                     foreach (var errorMsg in error.ValidationErrors)
11                     {
12                         // logging service based on NLog
13                         Console.WriteLine($"errorMsg: {errorMsg.ErrorMessage}; PropertyName = {errorMsg.PropertyName}");
14                     }
15                 }
16 
17                 throw;
18             }
19             catch (DbUpdateException e)
20             {
21                 foreach (var result in e.Entries)
22                 {
23                     Console.WriteLine($"Type: {result.Entity.GetType().Name} was part of the problem. ");
24                 }
25                 throw;
26             }
27             catch (Exception e)
28             {
29 
30                 throw;
31             }
View Code