Saltar apartados

Validation model BD

The validation on models of the BD is very alike that on the classes created by us. Before we have seen that to be able to define the rules of validation, accessed to the class, incorporated a series of spaces of names for afterwards go defining the characteristics of each one of the properties.

This complicate with the model Entity Framework. The file generated is very extensive, and besides is very complicated to find our properties. Besides it complicates something more if we know that every time that we do a change in the structure of the model in the own BD, and update the model in Visual Studio, go back to generate completely this file.

Therefore it is convenient to take out the rules of validation of the model and keep it aside.

Therefore inside the folder Models, go to create so many classes like models want to validate. We can not define the class such cual, because it would go in in conflict by the generated by Entity Framework. We defined it of partial type.

Clase parcial para indicar las definiciones de los campos del modelo

Inside this class did not define at all because we do not go to modify it, only want to define his attributes of validation. For this it is necessary to add a definition in the cabecera of the class, in which indicate that class goes to realise the validation. The normal is that this new class was inside the new class or at least in the same file, but out of the class partial. All the attributes seen previously are applicable for models generated with Entity Framework.

Clase para validar el modelo generado desde base de datos

Therefore we have a class and type partial with the same name that the model that want to validate and another class only to define the attributes. If we want to indicate that the field DESCRIPCION_IS is compulsory

Esquema básico de una clase que valida un modelo generado desde base de datos

The result would be the following

Ejemplo para validar un modelo generado a partir de una base de datos

Computing service ASP.NET MVC 2 Framework

Universidad de Alicante
Carretera de San Vicente del Raspeig s/n
03690 San Vicente del Raspeig
Alicante (Spain)

Tel: (+34) 96 590 3400

Fax: (+34) 96 590 3464

For further information:, and for enquiries on this web server:

Carretera San Vicente del Raspeig s/n - 03690 San Vicente del Raspeig - Alicante - Tel. 96 590 3400 - Fax 96 590 3464