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 ?
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 ?
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]
.
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.