I'm wondering what's the best method for validating a view field value to be unique in an entityset: before or after the update to persistence layer? The involved db field has an unique constraint, and its table is mapped to an EF model. I see two ways for unique value validation in an entityset:
- before saving changes to db (during model update or by decorating with custom DataAnnotations the model)
- after saving changes to db (by handling in the repository or controller the UpdateException generated by the persistence layer)
With the 1st method I need to query the db for checking the uniqueness, so any view update will require both a db select and a db update.
With the 2nd method, the additional select is not required, but it is difficult to identify the error type and the offending field.
I would prefer method 2, but the problem for determining if the insert/update failed due to a unique constraint force me to choose method 1.
Or is there another way?