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 the 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.
- [DisplayName("First Name :")]
- [Required]
- [StringLength(50, MinimumLength = 3, ErrorMessage = "First Name must be between 3 and 50 characters!")]
- public string FirstName { get; set; }
- //Foreign key for Standard
- public int StandardId { get; set; }
- public Standard Standard { get; set; }
- public int StandardId { get; set; }
- public string StandardName { get; set; }
- public class Student
- {
- public Student()
- {
- }

Santhakumar MunuswamyPosted May 17, 2015, 12:58 AM
Good