Muhammad Adyl's Blog

  • Home
  • About
  • Slides
  • Categories
    • ASP.Net MVC
    • Web API
    • Node.js
    • Angular.js
    • Android Development
    • NoSQLs
  • Contact

Implementing Data Annotation with Entity Framework

by Muhammad Adyl Wednesday, May 01, 2013 Code first Data Annotation Data annotation Schema EF 5 POCO
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

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:-
  1. CustomValidationAttribute - it uses a custom method for validation
  2. DataTypeAttribute - Use for data type validation, such as Email or Phone Number.
  3. EnumDataTypeAttribute - Validate data whether it exist in given Enum or not.
  4. RangeAttribute - Use for validating the range, minimum and maximum value that can be use.
  5. RegularExpressionAttribute - Use for validating data with the help of given Regular Expression.
  6. RequiredAttribute - Use where the value must be provided.
  7. StringLengthAttribute - Use for providing limit to string characters.
  8. 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:-
  1. DataTypeAttribute - Specifies a perticular type of data such as email or phone number.
  2. DisplayAttribute - Specifies a string for data type and members used that are used in user interface.
  3. 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:-
  1. KeyAttribute - use for defining unique key one or more time.
  2. 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 
  1. TableAttribute - Use for mapping class with Database table
  2. 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

Please free to comment any queries or complexities in blog.

READ MORE
SHARE :

Welcome To Blog








Muhammad Adyl


Microsoft Certified Trainer, and Technical Evangelist in AUC Technologies. Currently working as a Senior Software Engineer @ Afiniti.


Blog Archive

  • ▼  2016 (2)
    • ▼  March (1)
      • Exploring MVC 6
    • ►  January (1)
  • ►  2015 (1)
    • ►  August (1)
  • ►  2014 (1)
    • ►  January (1)
  • ►  2013 (3)
    • ►  May (2)
    • ►  March (1)

Follow Me

Labels

  • .net 4.5
  • .Net Core
  • ASP.NET5
  • Basic CRUD
  • Bower
  • Cloud optimized
  • Cloud-Optimized
  • Code first
  • Create View
  • Cross-Platform
  • Data Annotation
  • Data annotation Schema
  • Dependency Injection
  • DNX
  • easy web service
  • EF
  • EF 5
  • EF 7
  • EF without EF Data Context Scoffolding
  • Fiddler
  • Grunt
  • Gulp
  • Initializer
  • JIT
  • JSON
  • Jump Start
  • Linux
  • MacOS
  • Microsoft
  • MVC
  • MVC 4
  • MVC6
  • npm
  • NuGet
  • OpenSource
  • OWIN
  • POCO
  • Razor
  • RC1
  • Relation Less Model
  • RESTful
  • Roslyn
  • Seeding
  • SignalR 3
  • Visual Studio 2013
  • vNext
  • VS 2012
  • VS 2013
  • VS 2015
  • Web API
  • Web API 3
  • XML
  • Popular
  • Recent
  • Comments
    A basic guide to RESTful Web Service Using Web API 2
    Entity Framework 5: Code First Approach
    Jump Start MVC 4
    Exploring MVC 6
    Creating Simple CRUD (Create Retrieve Update Delete) MVC Application Through Model
    Implementing Data Annotation with Entity Framework
    vNext and ASP.NET 5, Microsoft's OpenSource and Cross-Platform technology.

About Me

My photo
Muhammad Adyl
· More than 5 years’ experience in Development · Proficient and Certified in Microsoft Technology, with some random experience over Android, Node.js, Angular.js, NoSQL, PHP, HTML5, CSS3 and JavaScript. · 2 publication · Volunteer Technology Speaker and Blogger.
View my complete profile

Popular Posts

  • Entity Framework 5: Code First Approach
    This is my first article of MVC 4 Development series, which is based on basic guidance of implementation of the code first approach, initi...
  • Creating Simple CRUD (Create Retrieve Update Delete) MVC Application Through Model
    For beginners, CRUD Applications are the applications that uses 4 operations for manipulating data. Those 4 operations are Inserting data, ...
  • Implementing Data Annotation with Entity Framework
    This post is a second part of the MVC 4 series that discusses implementation of Data Annotation . Before proceeding to the drill, I would f...

Labels

.net 4.5 .Net Core ASP.NET5 Basic CRUD Bower Cloud optimized Cloud-Optimized Code first Create View Cross-Platform Data Annotation Data annotation Schema Dependency Injection DNX easy web service EF EF 5 EF 7 EF without EF Data Context Scoffolding Fiddler Grunt Gulp Initializer JIT JSON Jump Start Linux MacOS Microsoft MVC MVC 4 MVC6 npm NuGet OpenSource OWIN POCO Razor RC1 Relation Less Model RESTful Roslyn Seeding SignalR 3 Visual Studio 2013 vNext VS 2012 VS 2013 VS 2015 Web API Web API 3 XML

Template Created By ThemeXpose & Blogger Templates