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

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

问题描述

我已经失去了这一点:



我已经为实体框架(4.1.3)定义了我的类代码第一种方法。一切都很好(我正在创建桌子等),直到我开始种子。



现在当我做

 添加迁移remigrate;更新数据库; 

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



我的Seed()方法中有一个断点,但是因为我在项目没有运行时在控制台上运行这个断点,对于如何获得详细信息(PS - 我已经看到线程可以使用实体框架保存对SQL Server数据库的更改,这显示了我如何查看属性。)



我知道我的Seed()方法有一个问题,因为如果我在方法调用之后放置一个返回值,错误消失。那么如何设置我的断点,所以我可以看到验证错误是什么?金达迷失了或者还有其他一些方法可以在nuget控制台中追踪?

解决方案

我最近也很烦躁。我通过在Seed方法的Configuration类中放置一个包装函数来修复它,并将调用替换为 SaveChanges 。此函数将简单地枚举 EntityValidationErrors 集合中的错误,并重新抛出异常,其中异常消息列出了各个问题。这使得输出显示在NuGet软件包管理器控制台中。



代码如下:

 code> ///< summary> 
///将SaveChanges的Wrapper添加到生成的异常中的验证消息
///< / summary>
///< param name =context>上下文< / param>
private void SaveChanges(DbContext context){
try {
context.SaveChanges();
} catch(DbEntityValidationException ex){
StringBuilder sb = new StringBuilder();

foreach(ex.EntityValidationErrors中的var failure){
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();
}
}

抛出新的DbEntityValidationException(
实体验证失败 - 错误如下:\\\
+
sb.ToString(),ex
); //将原始异常添加为innerException
}
}

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


I'm at a loss for this:

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.

Now when I do the

Add-Migration "remigrate" ; Update-Database;

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

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

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??

解决方案

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 follows:

/// <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
    }
}

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

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

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