从 DbValidationException 获取准确的错误类型 [英] Getting exact error type in from DbValidationException
问题描述
我遇到的情况是,我正在为 EF 4.1 在 DatabaseInitializer() 中初始化我的模型,并得到这个烦人的错误 一个或多个实体的验证失败.有关更多详细信息,请参阅EntityValidationErrors"属性.
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?
清除问题:
我知道如何解决字符串长度问题.我要问的是如何获得破坏模型的确切字段名称.
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.
推荐答案
当您在 catch {...}
块中处于调试模式时,打开QuickWatch"窗口 (ctrl+alt+q) 并粘贴在那里:
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
这将允许您深入查看 ValidationErrors
树.这是我发现的立即洞察这些错误的最简单方法.
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.
对于只关心第一个错误并且可能没有 catch
块的 Visual 2012+ 用户,您甚至可以这样做:
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屋!