EdB
EdB

Reputation: 449

How does one validate a partial record when using EF/Data Annotations?

I am updating a record over multiple forms/pages like a wizard. I need to save after each form to the database. However this is only a partial record. The EF POCO model has all data annotations for all the properties(Fields), so I suspect when I save this partial record I will get an error.

So I am unsure of the simplest solution to this.

Some options I have thought of:

a) Create a View Model for each form. Data Annotations on View model instead of EF Domain Model.

b) Save specific properties, rather than SaveAll, in controller for view thereby not triggering validation for non relevant properties.

c) Some other solution...??

Many thanks in Advance,

Upvotes: 1

Views: 66

Answers (1)

Jerad Rose
Jerad Rose

Reputation: 15513

Option 1. Validation probably (especially in your case) belongs on the view model anyway. If it is technically valid (DB constraint wise) to have a partially populated record, then this is further evidence that the validation belongs on the view.

Additionally, by abstracting the validation to your view, you're allowing other consuming applications to have their own validation logic.

Additional thoughts:

I will say, though, just as a side note that it's a little awkward saving your data partially like you're doing, and unless you have a really good reason (which I originally assumed you did), you may consider holding onto that data elsewhere (session) and persisting it together at the end of the wizard.

This will allow better, more appropriate DB constraints for better data integrity. For example, if a whole record shouldn't allow a null name, then allowing nulls for the sake of breaking your commits up for the wizard may cause more long term headaches.

Upvotes: 1

Related Questions