获取准确的错误从DbValidationException输入准确、错误、DbValidationException

由网友(流年染了我的年华)分享简介:我在哪里,我初始化我在DatabaseInitializer模型()用于EF 4.1的情况,并得到这恼人的错误验证失败的一个或多个实体。请参阅EntityValidationErrors属性的更多细节。 所以,我去这个EntityValidationErrors,有一个字段 {System.Data.Entity.Va...

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

要清除的事情了:

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

解决方案

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

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

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

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

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

阅读全文

相关推荐

最新文章