Model validation using Entity Framework
-
13-06-2021 - |
문제
I have a Entity Framework model class that has a view to create an instance of that model class. I wanted to validate dates on that class so that users add dates that meet certain criteria. I have a metadata class that uses attributes from the System.ComponentModel
and System.ComponentModel.DataAnnotations
namespaces.
Is there any attribute from there that I can use to perform the validation or is there some other way I can do this?
해결책
You'll need a CustomValidationAttribute, as existing attributes don't know their "object" (each property has no idea of other properties of the instance).
But my choice would go to FluentValidation, which has a nice fluent interface and give you the choice to interact between the properties of the object to validate.
다른 팁
You can use the CustomValidationAttribute
to do whatever additional validation you want. You'll have to work a bit harder if you need client-side validation to go with it though!