获取准确的错误从DbValidationException输入 [英] Getting exact error type in from DbValidationException

查看:347
本文介绍了获取准确的错误从DbValidationException输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在哪里,我初始化我在DatabaseInitializer模型()用于EF 4.1的情况,并得到这恼人的错误验证失败的一个或多个实体。请参阅EntityValidationErrors属性的更多细节。 所以,我去这个EntityValidationErrors,有一个字段 {System.Data.Entity.Validation.DbEntityValidationResult} 这给我任何信息的所有关于哪一个领域,它无法初始化。 有没有办法让这个错误的详细信息?

要清除的事情了:

我知道如何解决的字符串长度的问题。我问的是我如何才能被打破了模型的确切字段名。

解决方案

当你在抓在调试模式{...} 块开辟快速监视窗口( CTRL + <大骨节病> ALT + <大骨节病>问:),并在那里粘贴:

 ((System.Data.Entity.Validation.DbEntityValidationException)前).EntityValidationErrors
 

这可以让你深入到 ValidationErrors 树。这是我找到立竿见影了解这些错误的最简单方法。

对于Visual 2012+用户谁只关心第一个错误,可能不会有一个块,你甚至可以这样做:

<$p$p><$c$c>((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors.First().ValidationErrors.First().ErrorMessage

I have the situation where I'm initializing my model in DatabaseInitializer() for EF 4.1 and get this annoying error "Validation failed for one or more entities. See 'EntityValidationErrors' property for more details." So, I go to this EntityValidationErrors and there is a field {System.Data.Entity.Validation.DbEntityValidationResult} which gives me no information at all about what field it was unable to initialize. Is there a way to get more info about this error?

To clear things out:

I know how to fix the string length problem. What I'm asking is how do I get the exact field name that is breaking the model.

解决方案

While you are in debug mode within the catch {...} block open up the "QuickWatch" window (ctrl+alt+q) and paste in there:

((System.Data.Entity.Validation.DbEntityValidationException)ex).EntityValidationErrors

This will allow you to drill down into the ValidationErrors tree. It's the easiest way I've found to get instant insight into these errors.

For Visual 2012+ users who care only about the first error and might not have a catch block, you can even do:

((System.Data.Entity.Validation.DbEntityValidationException)$exception).EntityValidationErrors.First().ValidationErrors.First().ErrorMessage

这篇关于获取准确的错误从DbValidationException输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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