开发者

Different model validation scenarios

I have a user entity in my application where users input some basic information when they register to the application. If they want to use some advanced features they have to give full information.

So I have two validation scenarios.

My first approach was to exchang开发者_如何学Pythone the Required attribute with MyRequired attribute to avoid columns being created as NOT NULL in the database via Entity Framework.

But the model is validated if I add it to my DB context. So I can't add the entity if it's just filled with basic information.

Is there a way to have one entity with several different validation scenarios?


Is there any way to validate a model with different scenarious?

That's what view models are supposed to do. I would recommend you to avoid passing your EF models to the views. Also avoid passing EF domain models to your actions => always use view models. Those classes are specifically designed to meet the requirements of a given view, including validation attributes. Then map your model entities to your view models.

This way your domain models are completely decoupled from the way the information is being presented on a given view. Also (as it is your case) the same domain model could have two different representations on different views as well as different validation requirements of course => view models fill this gap.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜