Asp.Net MVC Entity Framework Database First Approach Example

Generally we will use entity framework database first approach to create business model from exists database. We will learn how to use entity framework database first approach in mvc application with simple example. To implement this we will use visual studio 2012 and Entity Framework 5.0.


Let's start with creating new mvc 4 application for that Open visual studio studio à Go to File à Select New à Select Project.


Creating new mvc database first approach application


After that you will see new dialog will pop up for selecting your Template and Project type. From Templates select Visual C# à inside that select Web and then project type select ASP.NET MVC 4 Web Application and here we are giving name as “MVCdatabasefirst” then finally click on ok button.


Database First Approach Entity Framework Application Creation in Asp.Net MVC


After naming it just click on OK now new dialog will pop up for selecting template in that Select Empty template and click ok like as shown below.


MVC Database First Approach in Entity Framework select empty template


After completion of creating application now it’s time to create database. For showing demo we already created a Database with name EmployeeDB and table with name EmployeeDetails like as shown below.


EmployeeDB Database


EmployeeDB Database in mvc application for database approach in entity framework


EmployeeDetails Table

Employee Details Table in Database First Approach in EF

Installing Entity framework

For adding Entity framework just right click on your application and from above list select “Manage NuGet Packages” like as shown below.


Installing Entity Framework in mvc application


After select a new dialog will popup of “Manage NuGet Packages” inside search box enter “Entityframework”. After getting search value select Entityframework click on install button.


Installing Entity Framework in mvc application


After adding it will show an ok sign in green color like as shown below.


Installed Entity Framework in mvc web application


After adding Entity framework now we are going to add ADO.NET Entity Data Model

Adding ADO.NET Entity Data Model

For adding ADO.NET Entity Data Model just right click on Model folder and select Add inside that Select ADO.NET Entity Data Model.


Selecting ADO.NET Entity Data Model in mvc applicaiton


After clicking on ADO.NET Entity Data Model a New Dialog will pop up for entering Item name inside that you can enter any name but it must be unique and click on OK button like as shown below.


Giving Name to model in mvc application


After that a new Wizard will popup like as shown below.


Choose Model Content for model in database first approach


From that select Generate from database and click on Next button. After clicking on Next button a New Wizard will pop up for Choosing Data Connection.

Choosing Data Connection

Choosing New Database Connection in Database First Approach in mvc


Now click on New Connection a new Dialog will popup. Here we need to configure it. In Server name you need to add your Sql Server Name and select either Using Windows Authentication or Using Sql Server Authentication to connect SQL Server. Here we selected Using Sql Server Authentication and entered User name and Password of Sql server. Last we are going to select Database Name: - EmployeeDB once we done click on OK button as shown below.


Configure Database Connection in Database First Approach in Asp.Net MVC Applicaiton


After adding database connection our Entity Data Model Wizard will look like below snapshot.


Entity Data Model Wizard with Database conneciton in mvc application


Now click on Next button. A new wizard will pop up for selecting database object and in this you will see all the table which we have created in database.


Choose Table Database in Database first approach in mvc application


And last click on Finish button. Here is snapshot after adding ADO.NET Entity Data Model.


Project structure after adding entity model in mvc applicaiton


Now let test how it works.

Adding Controller (Default1Controller)

To add controller Just Right click on Controller Folder inside that select Add and then select Controller like as shown below.


Adding new controller in mvc database first application


After clicking on Controller new dialog will pop up with name Add Controller in that mention controller name and select template type and click Add like as shown.


Adding new controller in mvc database first application


In this Default1Controller write code to get Employeelist from EmployeeDB database like as shown below.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

using MVCdatabasefirst.Models;


namespace MVCdatabasefirst.Controllers


public class Default1Controller : Controller


public ActionResult Index()


using (var ctx = newEmployeeDBEntities1())


List<EmployeeDetail> Employeelist = ctx.EmployeeDetails.ToList();

return View(Employeelist);





Now open our database table "EmployeeDetails" and enter some temporary data in it like as shown below.


Inserted user details in database table


Now let Test Database first approach. Here we showed snapshot in debugging mode.


Output of Database first approach in entity framework in mvc application

If we create view for this controller and bind values our output will be like as shown below.


Output of Database First Approach Entity Framework Application Creation in Asp.Net MVC