Entity Framework 4 Code First allows a highly productive convention based approach to developing our data models in an ASP.NET MVC application. This approach enables existing .NET developers to leverage their current skill-sets and quickly get up and running on entity framework and asp.net mvc. One of the conventions of EF 4 Code First is the usage of simple C# POCO objects to model our database entity models and entity relationships.
For our discussion, let’s say we want to write a simple blog application in ASP.NET MVC 2. So we come up with two entities Post and Comment and the relationships as follows:
- A Post has zero or many Comments.
- A Comment must belong to at most and at least one Post.
This is how we model the above scenario in EF 4 Code First:
public class Post
{
public int Id { get; set; }
[Required(ErrorMessage = "Title is required.")]
public string Title { get; set; }
[Required(ErrorMessage = "Body is requried.")]
public string Body { get; set; }
[Required(ErrorMessage = "Created By is required.")]
public string CreatedBy { get; set; }
public DateTime CreatedOn { get; set; }
public virtual ICollection<Comment> Comments { get; set; }
}
public class Comment
{
public int Id { get; set; }
[Required(ErrorMessage = "Name is Required.")]
public string Name { get; set; }
public string Url { get; set; }
public string CommentText { get; set; }
public bool? RememberMe { get; set; }
public virtual Post Post { get; set; }
}
Note the line number 16 and 29 above. For our Post entity we have many comments so we use an instance of ICollection to hold our Comments. For our Comment entity, since we need to specify that a comment belongs to at least one post we use an instance variable of Post entity. This is pretty much all we have to do to enable mapping of associations in our code model. No XML configuration file, no fluent mapping, no SQL scripts and so on. Easy, peasy! Agree?
On the database side of things, the EF 4 Code First generates for us our database tables and foreign key relationships among entities based on our POCO models. Notice the Post_Id column in our Comments tables. This is our foreign key pointing to our Post entity. This is all done by EF 4 Code First.
Adding Further Constraints to our Model
Another data model constraint that we frequently need to use from time and again is the Nullable and Not Nullable constraint.
-
.NET reference instance types are mapped as Nullable by default. In order to specify that the field in required, we need to use the Required attribute on the property. This is defined in the namespace
System.ComponentModel.DataAnnotations. See the lines 5, 8 and 11 in our Post model and how we specified that they are required fields. The figure on the left shows the resulting mapped database table. - .NET value types are mapped as Not Nullable columns in our database. To specify these fields as nullable we need to use the nullable keyword ‘?’ when declaring our model property. See line 27 in our Comment model above. The figure on the left is our resulting mapped database table.
Gotchas
As of EF 4 CTP 4.0 specifying the foreign key as required is not supported. So Post_Id is always mapped as Nullable in our Comments tables.


