Conditional Validation on MVC3 Model Class

前端 未结 1 1958
悲哀的现实
悲哀的现实 2021-01-25 09:20

I\'m using Entity Framework and a Model class, DonationForm, wrapped by a view model class \"CreateDonationForm\".

In keeping with the DRY principle, I have added the

1条回答
  •  攒了一身酷
    2021-01-25 10:00

    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.

    0 讨论(0)
提交回复
热议问题