When I want to use db.SaveChanges(), if some of the columns got validation error and throw DbEntityValidationException, and you can't tell which one is wrong, maybe try this way will help.

You can extract all the information from the DbEntityValidationException with the following code (you need to add the namespaces: System.Data.Entity.Validation and System.Diagnostics to your using list):

try
{
db.SaveChanges();
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationErrors in dbEx.EntityValidationErrors)
{
foreach (var validationError in validationErrors.ValidationErrors)
{
Trace.TraceInformation("Property: {0} Error: {1}",
validationError.PropertyName,
validationError.ErrorMessage);
}
}
}

Using debug tools, set breakpoints, then you will see the detail error message during the foreach loop.

How to handle the DbEntityValidationException in C#的更多相关文章

  1. c#+handle.exe实现升级程序在运行时自动解除文件被占用的问题

    我公司最近升级程序经常报出更新失败问题,究其原因,原来是更新时,他们可能又打开了正在被更新的文件,导致更新文件时,文件被其它进程占用,无法正常更新而报错,为了解决这个问题,我花了一周时间查询多方资料及 ...

  2. Nodejs事件引擎libuv源码剖析之:句柄(handle)结构的设计剖析

    声明:本文为原创博文,转载请注明出处. 句柄(handle)代表一种对持有资源的索引,句柄的叫法在window上较多,在unix/linux等系统上大多称之为描述符,为了抽象不同平台的差异,libuv ...

  3. 错误:java.util.Map is an interface, and JAXB can't handle interfaces.

    问题: 在整合spring+cxf时报错java.util.Map is an interface, and JAXB can't handle interfaces. 解决方法: 将服务端的serv ...

  4. "SQL Server does not handle comparison of NText, Text, Xml, or Image data types."

    "SQL Server does not handle comparison of NText, Text, Xml, or Image data types." sql2000 ...

  5. Handle类与线程

    首先声明Handle对象和该类的handleMessage方法: Handler BarHandler = new Handler(){ @Override public void handleMes ...

  6. Handle机制的原理

    Android提供了Handle和Looper来满足线程间的通信.Handle先进先出原则.Looper类用来管理特定线程内对象之间的消息交换(Message Exchange). 1.Looper: ...

  7. Record:Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView

    Handle onClick for our custom LinearLayout for Gallery-like HorizontalScrollView   The post "Im ...

  8. 独自handle一个数据库大程有感

    这学期数据库课程,最后的大程是写一个MiniSQL的数据库实现,要求很简单,建删表,建删单值索引,支持主键和unique定义,支持最简单的select,只要支持3个类型:int,float,char( ...

  9. Google V8编程详解(三)Handle & HandleScope

    上一章简单的演示了一个Helloworld Demo.里面涉及到了V8的一些基本类型和概念,本章将围绕这个Demo对V8的基本类型和相关概念进行讲解. 这里还是先把Demo贴出来便于后面分析: #in ...

随机推荐

  1. inuitcss

    inuitcssa powerful, scalable, Sass-based, BEM, OOCSS framework.

  2. C# 导出 Excel 数字列出现‘0’的解决办法

    在DataGird的中某一列全是数字并且长度大于15的字符,在导出excel时数字列第15-18位全部为0. 解决办法:在需导出数字列前加入英文字符状态的单引号(‘ ), 如: <asp:Tem ...

  3. vimium快捷键列表

    最近越来越懒了,不想拿手去碰鼠标,就想这样放在键盘上,在MacOSX下基本的操作也都能实现了,Xcode也没什么问题,现在就是有个地方十分不方便,就是浏览网页的问题,不管怎么样都是需要鼠标来浏览网页, ...

  4. JavaScript高级程序设计9.pdf

    Number是数字值对应的引用类型 var numberObject=new Number(10); Number也重写了valueof().toLocaleString().和toString()方 ...

  5. codeforces 212E IT Restaurants(树形dp+背包思想)

    题目链接:http://codeforces.com/problemset/problem/212/E 题目大意:给你一个无向树,现在用两种颜色去给这颗树上的节点染色.用(a,b)表示两种颜色分别染的 ...

  6. h2 database

    java -cp h2-1.4.187.jar org.h2.tools.Shell -url jdbc:h2:file:~/.h2/hzhssh -user sa 如果有个数据库的文件名为:hzhs ...

  7. 600字读懂 Git

    译注:来自 Hacker School 的 Mary Rose Cook 实现了一个纯 JavaScript 写就的 Git:Gitlet,包含了最主要的一些命令.这个项目一是为了了解 Git 内部原 ...

  8. Call-time pass-by-reference has been deprecated

    Warning: Call-time pass-by-reference has been deprecated解决方法 第一种方法: 修改php.ini就可以了. 1. 在PHP.ini中搜索关键字 ...

  9. java通过解析文件获取apk版本等信息

    import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import ...

  10. 二进制文件和ASCII文件有何差别

    二进制文件和ASCII文件(即文本文件)的差别,对于和计算机亲近时间尚短的同学是个难题.本文用简单的样例,试图展示当中的道道,希望能对菜鸟们有些帮助. 1.一个样例:两种100000 有程序: #in ...