This post is a second part of the MVC 4 series that discusses implementation of Data Annotation. Before proceeding to the drill, I would first like explain, what Data Annotation is and used for. Data annotation is a set of classes that used with data models to define data in form of attributes.
Data annotation attributes can be categories into 3 types.
1) Validation Attributes
2) Display Attributes
3) Modelling Attributes
some of the validation attributes are:-
Data annotation attributes can be categories into 3 types.
1) Validation Attributes
2) Display Attributes
3) Modelling Attributes
Validation Attributes
Validation attributes are the attributes that are use for validating data, these controls are similar to validation controls in asp.net webforms but over here it is taken as attributes rather control.some of the validation attributes are:-
- CustomValidationAttribute - it uses a custom method for validation
- DataTypeAttribute - Use for data type validation, such as Email or Phone Number.
- EnumDataTypeAttribute - Validate data whether it exist in given Enum or not.
- RangeAttribute - Use for validating the range, minimum and maximum value that can be use.
- RegularExpressionAttribute - Use for validating data with the help of given Regular Expression.
- RequiredAttribute - Use where the value must be provided.
- StringLengthAttribute - Use for providing limit to string characters.
- ValidationAttribute - Use has a base class for validation attributes.
Display Attributes
Display Attributes defines how data is being displayed from Data Model.
Some of the Display attributes are:-
- DataTypeAttribute - Specifies a perticular type of data such as email or phone number.
- DisplayAttribute - Specifies a string for data type and members used that are used in user interface.
- DisplayFormatAttribute - Specifies how data field is displayed or formatted.
Data Modelling Attributes
Data Modelling Attribute is used for specifying data members and the relation in between them.
Few of the main attribute of this category are:-
- KeyAttribute - use for defining unique key one or more time.
- TimestampAttribute - Use for defining a timestamp for data versioning.
These are the main attributes that we can find in System.ComponentModel.DataAnnotations Namespace, however there is also another namespace within System.ComponentModel.DataAnnotations for Schema definition i-e, System.ComponentModel.DataAnnotations.Schema .
I will only highlight few of the schema's attributes that are related to my example which you will see in drill.
Those are
- TableAttribute - Use for mapping class with Database table
- ColumnAttribute - Use for representation of the Column in Database Table.
For the implementation I have used my previous project of Restaurant Management System
Open each of the model file from your previous project add following attributes as shown below
Always remeber that these Attributes while implimenting doesnot need full name for example RequiredAttribute will be written like [Required] without keyword Attribute.
Rest of the Code remains same.
After Running a project you will get following changes in your database that can be viewed from SQL Object Explorer
Rest of the Code remains same.
After Running a project you will get following changes in your database that can be viewed from SQL Object Explorer
Please free to comment any queries or complexities in blog.
COMMENTS