Earlier this month we discussed the advantages of implementing the Repository pattern within your architecture. In this post we will take things a step further down our Multi-Tier Architectiure and discuss the Data Store within our Data Access Layer. We will take a look at the two most common approaches used in creating an Entity Framework conceptual model on top of your relational Database, Code First and Model first. 

To start off it is worth mentioning that the Entity Framework 4.0 actually provides three distinctive development approaches when it comes to data access within your web site:

  • Model First
  • Code First
  • Database First

In this article we'll only tackle the two most recent approaches, Code First and  Model First, the main reason being the fact that the Database First approach  has been used for a very long time and most web developers can easily point out the advantages and disadvantages of this approach. 

Model First

The Model First Approach was provided by Microsoft because it was one of the most often requested features by .Net Developers and System Designers. It is a more natural way to work compared to the Database First approach especially when designing the flow of data in the initial stages of a new project. Developers requested the flexibilty of creating the conceptual model first instead of going through the burden of outlining the project logic and then trying to design a database which would accomodate for all the data and information storage within the project. Using the Model First approach a developer can start working with the model of the databse and creating entities which make logical sense irrelevant of how they will actually be stored in the database in terms of tables.

From the model created, Visual Studio (2010 onwards) can then generate SQL statements referred to as Data Defenition Language (DDL) which the developer can use to execute on MS SQL in order to create the Database schema based on the designed model. 

With Model First, You Design a Model that's Used to Generate Database Schema

It is vital to point out that the Model First approach does not by deafult perform incremental schema updates on your database, so if the model is changed or updated for some reason, a new script must be generated which will overwrite the existing database schema. This will obviously result in a loss of data, however there is nothing to worry about as there are a number of datbase tools available which will help you update your database schema without compromising your data such as the Entity Designer Database Generation Power Pack from Microsoft.

Code First

The Code first approach is the most recent approach provided by Microsoft as part of the Entity Framework 4.0. Its a more developer oriented approach where a developer can literally code the entities required within the system as if they were regular classes and objects and then create the database schema from these classes by defining special relationships and mappings within these entities. The main advantages in utilizing the Code First  approach is that the developer has complete control on the relations between the entities (somthing which is not utterly provided by the Model first approach because of automatically generated code) and in the mean time still use an in-memory model that the EF runtime can work with. Using the code first approach the developer can also provide restrictions and annotations on specific properties in the domain models (classes) which will serve as validations both on the database and also within the code itself. 

The example below shows a typical domain model with data annotaions on properties and some navigation properties to other domain models:

[Table("SalesOrderDetail", SchemaName="SalesLT")]
public partial class Detail
{
// Scalar properties [Column(Name = "SalesOrderID")] public int OrderId { get; set; } [Column(Name = "SalesOrderDetailID")] public int DetailId { get; set; } public short OrderQty { get; set; } public int ProductId { get; set; } public decimal UnitPrice { get; set; } public decimal UnitPriceDiscount { get; set; }
public decimal LineTotal { get; set; } public System.DateTime ModifiedDate { get; set; }
// Navigation properties public virtual Product Product { get; set; } public virtual Order Order { get; set; }
}

The 'DbContext' class is then used to specify how the classes will be created and translated into database tables and including the relationships between the data entities.

How to choose which approach to take?

It all depends on the specific web development project requirements and the set of skills you've got at your disposal within your team really. System Designers tend to prefer the Model First approach because it allows you to take into perspective the whole system and the relations amongst all the models and entities within the system without having to deal with how the data will eventually be stored or the technicalities of data annotations within the code. On the otherhand developers will justly argue that Code First gives you more control on the relations and specifications of your domain model without compromising the advantages of Conceptual Mapping within your system. 

A very good decision tree I came across when I was first trying to figure out which approach suits us better at Incredible Web for certain projects is the diagram below:

a decision tree to help choosing different approaches with EF

Before commiting to a particular workflow a developer/system designer must go through the mentioned decisions within this decision tree. Hopefully it will make it easier for you to choose the right approach, I know it worked quite well for us.

Thanks for reading,

Shaun