首页 > 其他 > 详细

EntityFramework 数据校验异常处理

时间:2018-08-30 15:39:09      阅读:203      评论:0      收藏:0      [点我收藏+]
 1 public void Insert(PageHost entity)
 2 {
 3     try
 4     {
 5         db.pagehost.Add(entity);
 6         db.SaveChanges();
 7     }
 8     catch (DbEntityValidationException ep)
 9     {
10         CatchException(ep);
11     }
12     catch (Exception ep)
13     {
14         throw ep;
15     }
16 }
17  
18 private void CatchException(DbEntityValidationException ep)
19 {
20     StringBuilder sb = new StringBuilder();
21     foreach (DbEntityValidationResult item in ep.EntityValidationErrors)
22     {
23         foreach (string pp in item.Entry.OriginalValues.PropertyNames)
24         {
25             sb.AppendLine(item.Entry.Member(pp).CurrentValue.ToString());
26         }
27         foreach (DbValidationError i in item.ValidationErrors)
28         {
29             throw new Exception(string.Format("{0}\t{1}\t{2}", i.PropertyName, i.ErrorMessage, sb.ToString()));
30         }
31     }
32 }

 

EntityFramework 数据校验异常处理

原文:https://www.cnblogs.com/yanglang/p/9560309.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!