Conditional Validation on MVC3 Model Class

大憨熊 提交于 2019-12-02 03:30:56

There is a lot of cool principles but sometimes small violation makes your life easier. Get rid of data annotations from your EF model and place them on your view model where they belong. You can still use IValidatableObject in view model and compose the validation from multiple reusable helper methods used by multiple view models (so you can still achieve DRY principle).

If you stubborn and really want to have validation in EF model turn off validation in EF and handle it in upper layer as you already do:

dbContext.Configuration.ValidateOnSaveEnabled = false; 

EF level validation is for simple scenarios where your validation rules do not change among operations.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!