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 asp.net 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 asp.net mvc 4 application for that Open visual studio studio à Go to File à Select New à Select Project.
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.
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.
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.
For adding Entity framework just right click on your application and from above list select “Manage NuGet Packages” like as shown below.
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.
After adding it will show an ok sign in green color like as shown below.
After adding Entity framework now we are going to add 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.
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.
After that a new Wizard will popup like as shown below.
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.
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.
After adding database connection our Entity Data Model Wizard will look like below snapshot.
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.
And last click on Finish button. Here is snapshot after adding ADO.NET Entity Data Model.
Now let test how it works.
To add controller Just Right click on Controller Folder inside that select Add and then select Controller like as shown below.
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.
In this Default1Controller write code to get Employeelist from EmployeeDB database like as shown below.
public class Default1Controller : Controller
public ActionResult Index()
using (var ctx = newEmployeeDBEntities1())
List<EmployeeDetail> Employeelist = ctx.EmployeeDetails.ToList();
Now open our database table "EmployeeDetails" and enter some temporary data in it like as shown below.
Now let Test Database first approach. Here we showed snapshot in debugging mode.
If we create view for this controller and bind values our output will be like as shown below.