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.

Key

This specifies the primary key of the Data Table.

TimeStamp

ConcurrencyCheck

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

Required

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

Range

Max/min Length

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

StringLength

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; }  
Table

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

Column

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.

ForeignKey

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. {   
  5.   
  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. }  
  4.   
  5. public class Standard  
  6. {  
  7. public Standard()  
  8. {   
  9.   
  10. }  
  1. public int StandardId { getset; }  
  2. public string StandardName { getset; }  
  3.   
  4. public ICollection<Student> Students { getset; }  
  5.   
  6. }  
NotMapped

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;} }  
InverseProperty

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:
https://msdn.microsoft.com/en-us/library/ee256141(VS.100).aspx

 

Up Next
    Ebook Download
    View all
    Learn
    View all