Where to put Data Annotation? View Model or Model?

8

Using Entity Framework Code First, with view model and model .

To create a column with varchar(2) , I need to put MaxLength(2) in model and view model ?

    
asked by anonymous 28.08.2015 / 21:32

2 answers

8

No

ViewModels does not serve to create entities in a database. [MaxLength] is just to specify the width of the column in bank. Therefore, [MaxLength] should only be used in Model .

For ViewModels , use [StringLength] .

    
28.08.2015 / 21:56
2

It depends on how you want to ensure this. It seems to me that you want to guarantee under all circumstances that this property and consequently the column of the database has a maximum of 2 characters. Then put it in model . It's worth all the application.

If you use view model , use it if you want only validation to be used in this circumstance. Obviously if there is a conflict between the two, if everything is correct, the model should prevail.

    
28.08.2015 / 21:55