在首先使用EF 5代码初始化数据库时,RequiredIf属性会出错

时间:2013-02-27 06:33:05

标签: entity-framework asp.net-mvc-4

首次使用EF5代码初始化数据库时,在实体中使用RequiredIf属性时出现以下错误。

System.Data.DataException: An exception occurred while initializing the database. 
See the InnerException for details. ---> System.Data.Entity.Validation.DbEntityValidationException:
Validation failed for one or more entities. See 'EntityValidationErrors' property for more details. 

如果我评论[RequiredIf]属性代码,请首先成功构建数据库。有什么想法吗?

enter image description here

更新

我单步执行调试器,发现它正在添加验证错误。如何在首先使用代码初始化数据库时关闭此功能。

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

播种期间出现问题。 requiredIf验证表明,如果推荐ApplicationStatus,即2,那么DecisionDate是强制性的。所以我在我的上下文中将DecisionDate属性添加到种子函数中并且它有效。谢谢大家无论如何都要看这个问题。

获得的经验教训是,如果您在域对象中放置验证,则相应地对数据进行种子设定。

enter image description here

相关问题