EF 代码优先:如何从 nuget 包控制台查看“EntityValidationErrors"属性? [英] EF Code First: How do I see 'EntityValidationErrors' property from the nuget package console?

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

问题描述

我对此感到茫然:

我已经为实体框架 (4.1.3) 代码优先方法定义了我的类.一切都很好(我正在创建表格等),直到我开始播种.

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."

我的 Seed() 方法中有一个断点,但是因为我在项目未运行时在控制台上运行它,所以我不知道如何获取详细信息(PS - 我已经看过线程 验证失败一个或多个实体,同时使用实体框架保存对 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 using Entity Framework which shows how I can see the property.)

我知道我的 Seed() 方法有问题,因为如果我在方法调用之后立即返回,错误就会消失.那么如何设置断点以便查看验证错误是什么?有点输了还是有其他方法可以在 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??

推荐答案

我最近也被这个惹恼了.我通过在 Seed 方法的 Configuration 类中放置一个包装函数来修复它,并将对 SaveChanges 的调用替换为对我的函数的调用.此函数将简单地枚举 EntityValidationErrors 集合中的错误,并在 Exception 消息列出各个问题时重新引发异常.这会使输出显示在 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.

代码如下:

/// <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
", 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:
" + 
            sb.ToString(), ex
        ); // Add the original exception as the innerException
    }
}

只需在种子方法中用 SaveChanges(context) 替换对 context.SaveChanges() 的调用即可.

Just replace calls to context.SaveChanges() with SaveChanges(context) in your seed method.

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

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