在保存时加入这一段,就可以查看error具体是哪里出错了。正式发布需要删除这段,try catch毕竟会影响性能
try { entity.SaveChanges(); } catch (DbEntityValidationException e) { string error = ""; foreach (var eve in e.EntityValidationErrors) { error += string.Format("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) { error += string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } throw new Exception(error); }