How to handle the DbEntityValidationException in C#

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.

时间: 2024-08-24 14:33:33

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

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

这学期数据库课程,最后的大程是写一个MiniSQL的数据库实现,要求很简单,建删表,建删单值索引,支持主键和unique定义,支持最简单的select,只要支持3个类型:int,float,char(0~255).最开始,考虑到数据库的运行时确定类型的特点,选择了运行时强大的C#,还能顺便集成进Linq.但是一周后发现C#操作对象二进制结构的能力几乎为0,在写BufferManager的时候也发现完全不能自由的控制对象生命周期,并且IDisposable的实现也过于迷,与强大运行时和linq相比

Thread ID vs Pthread Handle(pthread_t)

在很多线程实现的案例中,pthread_t作为抽象类型,被指定为长度为4的整行作为线程ID. 在一些iSeries服务器的线程实现上,线程ID是一个64位的整型数值和pthread_t是一个抽象 的结构体包含数值和一些其他的参数.抽象化允许进程容纳上千个线程. 如果不允许移植,不允许进程访问pthread_t的内部数据和尺寸,例如对比线程ID,对于可移植的对比, 使用pthread_equal函数.文档通常描述pthread_t作为线程句柄,避免因为使用一个整型表示线程ID 而混淆概念, Thr

[object-c 2.0 程序设计]object-c file handle (二)

// // main.m // cmdTry // // Created by Calos Chen on 2017/8/21. // Copyright © 2017年 Calos Chen. All rights reserved. // #import <Foundation/Foundation.h> #import "class4.m" #import "NSObject_class5.h" @class Fraction; @interfac

Delphi使用普通类对象创建接受window消息(使用Classes.AllocateHWnd为对象创建一个尺寸为0的窗口,从而有了Handle)good

在delphi中,有时候我们希望对象可以接收windows消息,怎么办呢?因为要接收windows消息起码要有windows Handle,难道要建立的一个可见窗口?那样似乎太差强人意了.delphi提供了一个函数Classes.AllocateHWnd.分析AllocateHWND发现delphi CreateWindowEx一个尺寸为0的窗口,窗口是生成了,Handle也有了,但窗口的消息要处理吧,否则怎么说让对象接收Windows消息呢,但我们都知道类函数和Windows消息处理函数是不一

Get a handle on PHP Handlers

PHP Handlers? mod_php? FPM? How do we make sense of the inner workings of PHP outside of our lines of code? We know we can run PHP on the server to build web applications swiftly, but how can we optimize our environment and configurations to reach ma

UI线程处理Handle

android的UI是不可以在子线程中更新,因为子线程涉及到UI更新,,Android主线程是线程不安全的,也就是说更新UI只能在主线程中更新,但是在主线程中更新如果更新超过5秒钟,android系统就会收到android系统的一个错误提示"强制关闭",这个时候Handle就出来了,由于Handler运行在主线程中(UI线程中),  它与子线程可以通过Message对象来传递数据, 这个时候,Handler就承担着接受子线程传过来的(子线程用sedMessage()方法传弟)Messa

Android开发之bug-No Activity found to handle Intent

android.content.ActivityNotFoundException: No Activity found to handle Intent 做Android开发中,使用隐式intent,Manifest中设置了action和category, 如下: <activity android:name="exerciseintent.ExIntent2_1"> <intent-filter > <action android:name="

How to handle exception in managed code and unmanaged code

As we known, try...catch mechanism is a quite common feature for the high level languages like java or C#. Although C++ proclaimed  that it supports this mechanism, the memory management limitation of C++ makes this try...catch function is weak. C++

闲话handle和handler

虽然handle和handler只有一个字符之差,但在程序员的世界里,含义却大相径庭. 1. 先说说handle 北京话说"一边儿玩儿去,玩勺子把儿去","勺子把儿"说的就是handle.二将handle翻译成"句柄"绝对是一个相当文雅相当阳春白雪的翻译,因为太文绉绉啦,很多文化底蕴不够的码农就看不大懂了或者望而生畏.为了弄明白为什么这么翻译,我费了点周折. 句柄者,弯杆杆儿,弯把手儿也.注意: "句"为"勾&quo