Data Annotation Attribute On Code First Approach

The Data Annotation attribute validates individual fields in the data model. The following is the list of the most-used Data Annotation attributes. The namespace for the Data Annotation is System.ComponentModel.DataAnnotations.


This specifies the primary key of the Data Table.



For an Update command in Entity Framework, it takes the value of the ConcurrencyCheck column in the Where Clause.


Entity Framework creates a non-null property in the database.


Max/min Length

It can be applied only to a string or array type of property in the Model Class.


Code First creates a fixed size of column in the StringLength. Here is a very interesting point. Then what's the use of max length? It is my understanding that the maxlength is used to create the column in the DB whereas Stringlength is used for client-side validation.

  1. [DisplayName("First Name :")]  
  2. [Required]  
  3. [StringLength(50, MinimumLength = 3, ErrorMessage = "First Name must be between 3 and 50 characters!")]  
  4. public string FirstName { get; set; }  

Attibute is used to create the table with the specified name.


Create a column with that name (the default is the model property name).

You can also specify an order and type of the column using the Column attribute.


By default the Foreignkey is set by the reference property as in the following:
  1. //Foreign key for Standard  
  2. public int StandardId { getset; }  
  3. public Standard Standard { getset; }  
  1. public int StandardId { getset; }  
  2. public string StandardName { getset; }  
But the Foreign key DataAnnotation can change the default behaviour as in the following:
  1. public class Student  
  2. {  
  3. public Student()  
  4. {   
  6. }  
  1. public int StudentID { getset; }  
  2. public string StudentName { getset; }  
  1. //Foreign key for Standard  
  2. public int StandardRefId { getset; }  
  1. [ForeignKey("StandardRefId")]  
  2. public Standard Standard { getset; }  
  3. }  
  5. public class Standard  
  6. {  
  7. public Standard()  
  8. {   
  10. }  
  1. public int StandardId { getset; }  
  2. public string StandardName { getset; }  
  4. public ICollection<Student> Students { getset; }  
  6. }  

Implementing this column the Default Column is not created.

Code First also does not create a column for a property that does not have either getters or setters as in the following:
  1. public string FirstName { getreturn StudentName;} }  
  2. public string Age { set{ _age = value;} }  

Code First creates a {Class Name}_{Primary Key} foreign key column if you have not included a foreign key property in the parent class. The InverseProperty attribute is used when you have multiple relationships among classes.

The Fluent API is another way to configure your domain classes. The Fluent API provides more capabilities for configuration than DataAnnotations. Fluent API supports the following types of mappings.

For a detailed understanding of the Data Annotation technique, please refer to the following link:


Up Next
    Ebook Download
    View all


    Read by 0 people
    Download Now!
    View all