EF code首先:我如何看到从包的NuGet控制台“EntityValidationErrors”属性? [英] EF Code First: How do I see 'EntityValidationErrors' property from the nuget package console?

查看:519
本文介绍了EF code首先:我如何看到从包的NuGet控制台“EntityValidationErrors”属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个茫然:

我定义我的班了实体框架(4.1.3)code第一种方法。一切都很好(我是创建表等),直到我开始播种。

I've defined my classes for a entity framework (4.1.3) code first approach. Everything was fine (I was creating the tables etc.) until I started to Seed.

现在,当我做了

Add-Migration "remigrate" ; Update-Database;

我得到一个错误的包控制台上的验证失败的一个或多个实体,参见EntityValidationErrors'属性的更多细节。

I get an error on the package console "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details."

我有一个断点在我的种子()方法,但因为我运行此项目不运行时在控制台上,我无能,如何才能到细节(PS - 我见过线程<一个href=\"http://stackoverflow.com/questions/5400530/validation-failed-for-one-or-more-entities-while-saving-changes-to-sql-server-da\">Validation失败的一个或多个实体,同时将更改保存到SQL Server数据库这说明我怎么能看到该属性)。

I have a breakpoint in my Seed() method but because I'm running this on the console when the project is not running, I'm clueless as to how to get to the details (PS - I've seen the thread Validation failed for one or more entities while saving changes to SQL Server Database which shows how I can see the property.)

我知道,因为如果我把一个返回的方法调用之后,错误消失我的种子()方法有问题。那么,如何设置我的断点,这样我就可以看到验证错误是什么?有点丢失。或者是有一些其他的方式来跟踪它在控制台的NuGet ??

I know that my Seed() method has a problem because if I put a return right after the method call, the error goes away. So how do I set my breakpoint so I can see what the validation error is? Kinda lost. Or is there some other way to trace it in the nuget console??

推荐答案

我被这惹恼最近太。我固定它通过把一个包装函数在配置类种子的方法,并替换为调用我的函数,而不是调用的SaveChanges 。该功能只会列举误差 EntityValidationErrors 集合中,并重新抛出在异常消息列出了个别问题的异常。这使得在的NuGet包管理器控制台输出显示。

I got annoyed by this recently too. I fixed it by putting a wrapper function in the Configuration class in the Seed method, and replaced calls to SaveChanges with calls to my function instead. This function would simply enumerate the errors within the EntityValidationErrors collection, and rethrow an exception where the Exception message lists the individual problems. This makes the output show up in the NuGet package manager console.

code如下:

/// <summary>
/// Wrapper for SaveChanges adding the Validation Messages to the generated exception
/// </summary>
/// <param name="context">The context.</param>
private void SaveChanges(DbContext context) {
    try {
        context.SaveChanges();
    } catch (DbEntityValidationException ex) {
        StringBuilder sb = new StringBuilder();

        foreach (var failure in ex.EntityValidationErrors) {
            sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
            foreach (var error in failure.ValidationErrors) {
                sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                sb.AppendLine();
            }
        }

        throw new DbEntityValidationException(
            "Entity Validation Failed - errors follow:\n" + 
            sb.ToString(), ex
        ); // Add the original exception as the innerException
    }
}

只需更换你的种子法 context.SaveChanges电话()的SaveChanges(上下文)

这篇关于EF code首先:我如何看到从包的NuGet控制台“EntityValidationErrors”属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆