Asp.Net MVC Entity Framework Database First Approach Example

We will generally use the entity framework database first approach to create a business model from the existing database. We will learn how to use entity framework database first approach in asp.net mvc application with a simple example. To implement this, we will use visual studio 2012 and Entity Framework 5.0.

 

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

 

Creating new asp.net mvc database first approach application

 

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

 

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

 

After naming it, click on OK a new dialog will pop up for selecting a 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 applications, it’s time to create a database. For showing demo, we already created a Database with the name EmployeeDB and a table with the name EmployeeDetails like as shown below.

 

EmployeeDB Database

 

EmployeeDB Database in asp.net 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, right-click on your application, and from the above list, select “Manage NuGet Packages” as shown below.

 

Installing Entity Framework in Asp.net mvc application

 

After selecting a new dialog, the “Manage NuGet Packages” will pop up inside the search box enter “Entityframework”. After getting the search value, select Entityframework click on the install button.

 

Installing Entity Framework in Asp.net mvc application

 

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

 

Installed Entity Framework in asp.net mvc web application

 

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

Adding ADO.NET Entity Data Model

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

 

Selecting ADO.NET Entity Data Model in asp.net 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 the OK button like as shown below.

 

Giving Name to Ado.net model in mvc application

 

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

 

Choose Model Content for Ado.net model in database first approach

 

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

Choosing Data Connection

Choosing New Database Connection in Database First Approach in Asp.net mvc

 

Now click on New Connection, a new Dialog will pop up, and you 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 the User name and Password of the SQL server. Last, we will select Database Name: - EmployeeDB. Once we have done, click on the OK button as shown below.

 

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

 

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

 

Entity Data Model Wizard with Database conneciton in asp.net mvc application

 

Now click on the Next button. A new wizard will pop up for selecting database objects, and in this, you will see all the tables that we have created in the database.

 

Choose Table Database in Database first approach in asp.net mvc application

 

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

 

Project structure after adding ado.net entity model in asp.net mvc applicaiton

 

Now let's test how it works.

Adding Controller (Default1Controller)

To add controller, right-click on Controller Folder inside that select Add and then select Controller like as shown below.

 

Adding new controller in asp.net mvc database first application

 

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

 

Adding new controller in asp.net 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 = new EmployeeDBEntities1())

{

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 a snapshot in debugging mode.

 

Output of Database first approach in entity framework in asp.net mvc application

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

 

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