Thursday, March 29, 2012

Entity Framework Code First: first glance

I’m currently developing small online store. Usually i use LINQ to SQL to work with database, but this time i decided to use Entity Framework Code First, since i found Code First approach more tidy and comfortable. So this post is about EF Code First and migrations.

First create new ASP.NET MVC 3.0 project. Name it, for example, EfSample. Now we need to add entity framework reference. Open Package Manager Console and type Install-Package EntityFramework - this will download and install latest version of entity framework.

Now let’s move to our database. For example, we want such database structure:

Now we need to write classes (code first!) to represent our database model. Add 3 classes in your Model folder:

As you can see this classes has no constraints (such as field requirement and database keys). We will add this stuff later, and use migrations.

Now we need to create database context, which will be used to query database. To simplify the example just add another folder called, for example, ‘ORM’ in project. And create an EfContext class in it:

The last thing to add will be connection string in web.config. The name of the connection string should match the name of context class:

This is all we need for now to generate our database. Now add HomeController with Index action and a view for this action. Let’s display some data from our database.There is no data, but nevermind - we just want to make a request to database, and see how entity framework will automatically generate it:

HomeController:
Index View:

After you run the application go to MSSQL Server Management Studio. You will find new database called EfTest:

As you can see the fields data types are default values, and all the fields, except keys, are nullable. Lets fix it. But first add some data to the EfTest database.

Now i’m going to rewrite Product, Manufacturer and Category classes and add some field constraints:

Now almost each field got data annotation. This is much better, than default values. But if you run the application once again you will see the following error: The model backing the 'EfContext' context has changed since the database was created. Consider using Code First Migrations to update the database.

It recommends to use migrations to update database model. Notice, that this update will be done without loosing data.

So now open Package Manager Console once again and type Enble-Migrations. This will create a new folder ‘Migrations’ with two files: Configuration.cs, and ..._InitialCreate.cs The first one describes context behaviour and the second represents currently existing database.

Since we added some field constraints we need to create another migration, based on the recent changes and update database. Type Add-Migration Constraints in Package Manager Console. Another migrations file called ..._Constraints.cs will be created. And now to update the database type Update-Database. Now refresh EfTest database in MSSQL Server Management Studio and you will see, that all the constraints applied to the database fields:

Now the database is updated without using designer, and you have a history of all updates. That's nice. :)

1 comment :

  1. nice post.its better if u can share the completed project to download

    ReplyDelete