Code First approach inside the Entity Framework.

In this article we will focus on Code First approach provided by the entity framework(EF). EF also has Database First and Model First approach apart from Code First approach. Code First approach is a process where in we create our own POCO classes and EF will create corresponding Database based on our classes, automatically.

Code First Approach in the Entity Framework

In this article lets get the required understanding of Code First approach and its uses in the application and how Entity Framework treats the user using Code First approach in their applications.

Lets get to the introduction and basic aim of Entity Framework first.

Introduction to Entity Framework

Microsoft has introduced Entity framework with an aim to make it a prefferable approach to access back end objects. EntityFramework(EF) is an abstract of ADO.NET. It is not a new technology but a layer created on top of ADO.NET. It is based on ORM(Object relational mapping) framework and gives the developer an automated mechanism for accessing and storing the data in the database.

Code First

As the name suggest, in Code First, we will create the Code First and based on our code EF will create the corresponding tables automatically. So a developer will start writing the code in C# without worrying or focusing on database design.

EF have introduced 3 approaches:
- Database First
- Model First
- Code First

Now lets start with working on to getting our first hands on Code First Approach.

The Examples used below are much simpler in nature to make it more understandable to the users.

POCO classes

Lets start the Code-First by creating classes(POCO - Plain Old CLR Objects)

public class Book
public int Id { get; set; }
public string BookName { get; set;}
public string BookPublication { get; set;}
public List Authors { get; set; }

public class Author

public int Id { get; set; }
public string AuthorName{ get; set; }
public string AuthorCompany { get; set; }

Web.Config changes

We need to define a connection string in the Web.Config file, so EF will be able to create and manage the Book and Author Details. A sample ConnectionString is shown below:

connectionString="Data Source=.;Initial Catalog=Books;Integrated Security=SSPI"

Creating the DBContext class and accessing it

In order to access the classes created by us, we need to create DBContext classes and obtain methods to access the POCO classes.

Following is the DBContext classes used:

public class BooksDBContext : DbContext
public DbSet Books { get; set; }
public DbSet Authors { get; set; }
public BooksDBContext()

Now lets create a class named as Books and retrieve the date from BooksDBContext

public class Books
BooksDBContext bookContext = new BooksDBContext();

public List GetBookDetails()
return (from b in bookContext.Books
select b).ToList();

When you debug your code you wil be able to see the list of Book details along with the Author information.

Controller class

To complete the task, lets create a controller to test the result passed in the list

public class BooksController : Controller
BooksDBContext bookContext;
public BooksController()
bookContext = new BooksDBContext();

public ActionResult Index()
var books = bookContext.Books.ToList();
return View(books);

public ActionResult GetAuthorInfo()
var books = bookContext.Authors.ToList();
return View(books);

Hence, using Code first approach in Entity Framework, we get the Book information and Author Information in the list and we can parse the list to the view and create a presentable UI to the end user.


Author: Phagu Mahato31 Dec 2013 Member Level: Gold   Points : 10

his model was firstly introduced in EF version 4.0 and we could start with a blank model and then create a database from that model.When we made changes to the model , we could recreate the database from the new model.

The Code First approach is the more code-centric than the other two. Basically we write POCO classes and then we persist to a database using something called DBContext. Code First relies on DbContext. We create two three classes with properties and then these classes interact with the DbContext class we can create a new database based upon our POCOS classes and have tables generated from those classes.We do not have an .edmx file in this approach.By using this approach we can write much easier unit tests.

DbContext is a new context class and is smaller,lightweight wrapper for the main context class which is ObjectContext . Let's move on to our hands-on example.

I have installed VS 2012 Ultimate edition in my Windows 8 machine.

1) Create an empty web application. Give your application a suitable name. Choose C# as the development language

2) Add a new web form item in your application. Leave the default name.

3) Create a new folder. Name it CodeFirst .

4) Add a new item in your application, a class file. Name it Footballer.cs. This is going to be a simple POCO class.Place this class file in the CodeFirst folder.

The code follows

public class Foot
public int FootballID { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public double Wgt { get; set; }
public double Hgt { get; set; }

The code for the class follows

public class DALfootball

FootballerDBContext ctx = new FootballerDBContext();

public List GetFootballers()

var query = from player in ctx.Footballers select player;

return query.ToList();



5) We will have to add EF 5.0 to our project. Right-click on the project in the Solution

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: