ASP.NET MVC Code First Approach

In this tutorial let us build an example application using ASP.NET MVC code first approach of entity framework. In our last tutorial, we have created the Code first example application, which is a console application. It is very important that you read that Tutorial first, before continuing with this Tutorial. We are going to use the same user domain class and concepts show you how to build a simple CRUD application using ASP.NET MVC.

Software Versions used in the tutorial

  • Visual Studio 2015
  • Entity Framework 6.2.0.
  • .NET 4.5
  • ASP.NET 4.5.2

Entity Framework is an ORM Framework. If you are new to Entity Framework you can find here the complete list of Tutorial on Entity Framework.

Create the ASP.NET MVC Project

  1. Open Visual Studio.
  2. File ->New -> Project
  3. Select Visual C# -> Web ASP.NET Web Application. Name the Project as EFInASPNETMVC and click on OK
  4. Select MVC
  5. Click on Change Authentication and select No Authentication
  6. Click OK

The above will install the default template.

Create ASP Net Code First MVC Application
Select MVC Option ASP Net  Code First MVC Application

Next, Open the Views\Shared_Layout.cshtml. Locate the “Application name” and “My ASP.NET Application” to “Code First in ASP.NET MVC”

Next, under the menu entry  add the following menu item for the user’s Menu 

Open the Views\Home\Index.cshtml. Replace the contents of the file with the following code

Run the application. The application should run without any error as shown below

Application Running ASP NET MVC Code First

Install Entity Framework

We looked at how to install the entity framework in the last tutorial. Go to the Package Manager Console and type the following command

This will install the latest version of the Entity Framework (6.2.0).

Entity model

The next step is to create the Entity Data Model (EDM).

  1. Select the Models folder
  2. Right click and click on Add->Class
  3. Name the file as model.cs
  4. Copy the user class which we created in the last tutorial.


The DbContext (often referred to as context) is the class which, helps us to query, insert, update and delete operations on the entities and persists them to the database, when required

Add the DBContext class to the Project using the following steps

  1. Select the Models folder
  2. Right Click -> Add -> Class
  3. Name the class as EFContext.cs
  4. Click on Add
  5. Import the namespace System.Data.Entity
  6. Make the EFContext class as public and inherit it from the DbContext class
  7. Add the Dbset Property for the user entity type

User Interface

Now let us build a simple UI Screen for our user table.

  1. Select the Controller folder
  2. Click on Add Controller.
  3. Select MVC 5 Controller with views, using Entity Framework
Create MVC Controller with Views using EF

Click on Add to go to the next dialog. In the Add Controller dialog

  1. Select Model class as User (EFInASPNETMVC.Models)
  2. Select Data Context class as EFContext (EFInASPNETMVC.Models)
  3. Enter the controller name as UsersController and click on Add
Create MVC Controller Add Controller Dialog

The Visual studio scaffolding framework does an excellent job of creating CRUD operations based on our model classes.

Run The Application

Run the application. Click on the user’s menu. Add the new users using the Create new user link.


The Code First generates the database in App_data folder. The name of the database is “EFInASPNETMVC.Models.EFContext” which is the fully qualified name of our DBContext class.

CRUD Operations

The ASP.MVC Does a good job of creating CRUD Operation by scaffolding the model class. Let us look at the generated code. Goto Controller folder and open the UsersController.

The Context class is initialized at the top of the class

The Index Action method returns the list of Users by invoking the ToList method of the Users DbSet.

The Details Actions method uses the Find method to retrieve the selected User from the database and converts it into an instance of the user entity.

The Create Action method invokes the Add method of the Users DbSet to add the user to the DbSet. The SaveChanges method of the context class is called next to persist the data back to the database

The Entities needs to be added to the context in order to perform CRUD Operation. Here the user instance received is added to the context using the Entry method. The state of the Entity is also changed to Modified indicating that it is modified. Finally, SaveChanges is called to persist the data 

Finally, the Delete Action method. The entity to be deleted is queried using the Find method. Next, it is removed from the context using the Remove method. The SaveChanges updates the database

The above example code gives you a brief introduction of how CRUD operations work in ASP.NET MVC Code first applications. You can learn more about them by referring to the following tutorials



In the last tutorial, we used the console application to show you how to use code first in an application. In this tutorial, we demonstrated how to build a Sample ASP.NET MVC application using the code first approach. In the next tutorial let us look at how to configure the database in Code first approach.


Source Code

3 thoughts on “ASP.NET MVC Code First Approach”

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top