博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

Validation failed for one or more entities.

Posted on 2015-05-20 14:17  生命如风  阅读(180)  评论(0编辑  收藏  举报
 验证失败后用DbEntityValidationException 查找出错的字段

try { // Your code... // Could also be before try if you know the exception occurs in SaveChanges int effect = await _unitOfWorkAsync.SaveChangesAsync(); } catch (System.Data.Entity.Validation.DbEntityValidationException e) { foreach (var eve in e.EntityValidationErrors) { Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw; }