When using the entity Framework Database frist or model first, it is too realistic and unreasonable to directly add attribute to the Modle class, because the model class is automatically generated and will overwrite its own modifications when regenerated. A more reasonable approach is to use the partial class approach to achieve.
For example, there is a model class: Movie. Then we can add a local class file, the contents of the local class are as follows:
usingSystem.ComponentModel.DataAnnotations;namespacemovies.models{[Metadatatype (typeof(Moviemetadata))] Public Partial classMovie {classMoviemetadata {[Required (errormessage="Titles are required")] Public stringTitle {Get;Set; } [Required (ErrorMessage="The price is required.")] [Range (5, -, errormessage ="Movies cost between $ and $.")] Public decimalPrice {Get;Set; } } }}
This is done by adding the required validation attributes to the local class file.
Example 2:
[Metadatatype (typeof(Fileuploadermetadata))]//data validation with Fileuploadermetadata for Fileuploader Public Partial classfileuploader:ibaseentity {#regionCustom attributes, which are entities extended by the data entity#endregion } Public classFileuploadermetadata {[Scaffoldcolumn (false)] [Display (Name="PRIMARY Key", Order =1)] Public ObjectId {Get;Set; } [Scaffoldcolumn (true)] [Display (Name="name", Order =2)] [Stringlength ( $, errormessage ="length must not exceed")] Public ObjectName {Get;Set; } [Scaffoldcolumn (true)] [Display (Name="Path", Order =3)] [Stringlength ( $, errormessage ="length must not exceed")] Public ObjectPath {Get;Set; } }
Mvc+entity Framework database First, the generated model updates all of the model each time a table is updated