i have a problem with my mvc3 application, i need to create some Validation stuff. but i dont have a normal model class because i have used EF with the database first approach.
i can not just open the class and write something like:
[Required]
[StringLength(10)]
how to solve this problem?
You could use the MetadataType attribute. So let’s suppose that EF spit at your face the following class which you cannot modify:
Now you could extend it since it is a partial class:
and then define the metadata:
Or, simply throw data annotations away and use a real validation framework such as FluentValidation.NET (which I use and strongly recommend) allowing you to express complex validation rules on your models in a fluent manner, has an excellent integration with ASP.NET MVC and also allows you to elegantly unit test your vaildation logic in isolation.