Code First Model Data Annotations
Accepting Null
public int? RequisitionID { get; set; }
Identity
[Key]
public string ISBN { get; set; }
Field Length
[StringLength(256)]
public string Title { get; set; }
Range
[Range(1, 100, ErrorMessage = "Price must be between $1 and $100")]
MaxLength and MinLength
[MaxLength(10),MinLength(5)]
public string BloggerName { get; set; }
[StringLength(5)]
Compare
Some time you would like to compare value of one field with other field, we can use the “Compare” attribute.
public string Password { get; set; }
[Compare("Password")]
public string ConfirmPass { get; set; }
One to Many Relation
[RelatedTo(RelatedProperty=”Author”)]
public ICollection<Book> Books { get; set; }
Foreign Key
[RelatedTo(RelatedProperty=“Books”, Key=”AuthorSSN”, RelatedKey=”SSN”)]
public Person Author { get; set; }
Required
[Required]
public string Title { get; set; }
[Required(ErrorMessage = "Genre must be specified")]
Date Related
[DisplayFormat(DataFormatString = "{0:d}")]
public DateTime ReleaseDate { get; set; }
[DataType(DataType.Date)]
public DateTime ReleaseDate { get; set; }
NotMapped
Code first convention dictates that every property is represented in the database. But this isn’t always the case in your applications. For example you might have a property in the Blog class that creates a code based on the Title and BloggerName fields. That property can be created dynamically and does not need to be stored. You can mark any properties that do not map to the database with the NotMapped annotation such as this BlogCode property.
[NotMapped]
public string BlogCode{
get{
return Title.Substring(0, 1) + ":" + BloggerName.Substring(0, 1);
}
}
Table
Specify name of the DB table which will be mapped with the class
[Table("StudentInfo")]
Column
The Column annotation is a more adept in specifying the attributes of a mapped column. You can stipulate a name, data type or even the order in which a column appears in the table. Here is an example of the Column attribute.
[Column(“BlogDescription", TypeName="ntext")]
public String Description {get;set;}
Linking Tables
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
public List<Post> PostsWritten { get; set; }
public List<Post> PostsUpdated { get; set; }
}
Relationship Attributes: InverseProperty and ForeignKey
Code first convention will take care of the most common relationships in your model, but there are some cases where it needs help.
Changing the name of the key property in the Blog class created a problem with its relationship to Post.
When generating the database, code first sees the BlogId property in the Post class and recognizes it, by the convention that it matches a class name plus “Id”, as a foreign key to the Blog class. But there is no BlogId property in the blog class. The solution for this is to create a navigation property in the Post and use the Foreign DataAnnotation to help code first understand how to build the relationship between the two classes —using the Post.BlogId property — as well as how to specify constraints in the database.
public class Post
{
public int Id { get; set; }
public string Title { get; set; }
public DateTime DateCreated { get; set; }
public string Content { get; set; }
public int BlogId { get; set; }
[ForeignKey("BlogId")]
public Blog Blog { get; set; }
public ICollection<Comment> Comments { get; set; }
}
Regural Expressions
In case you want to use regular expression, you can use “RegularExpression” attribute.
[RegularExpression(@"[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}")]
public string Email { get; set; }
How can we enable data annotation validation on client side?
It’s a two-step process first reference the necessary jquery files.
<script src="<%= Url.Content("~/Scripts/jquery-1.5.1.js") %>"
type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.validate.js") %>"
type="text/javascript"></script>
<script src="<%= Url.Content("~/Scripts/jquery.validate.unobtrusive.js") %>"
type="text/javascript"></script>
Second step is to call “EnableClientValidation” method.
<% Html.EnableClientValidation(); %>