Entity Framework - SPLessons

Entity Framework DataBase First Approach

Home > Lesson > Chapter 8
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Entity Framework DataBase First Approach

Entity Framework DataBase First Approach

shape Introduction

Follow the below steps to work with Database First Approach.Create an MVC Application.If don't know "How to create MVC Application", refer the ASP.NET MVC 5 lesson. Creation of the database is already done which is used earlier i.e Employee and Department tables.

shape Step-1

Follow the steps in the previous chapter to create an Entity Framework. But here there is no need of two tables. Only employee table is enough. After Creation of Entity Framework, the diagram will be displayed as shown in the below figure.

shape Step-2

Now go to Solution Explorer->Right Click on the Controllers folder -> Add ->Controller. Then the following window will appear.

shape Step-3

Select the MVC Empty controller and click on Add as marked in the above figure. Then the following window will appear. Give the Name to the controller without deleting the term Controller and then click on Add. Then the code will be generated in the EmployeeController.cs as shown in the below figure.

shape Step-4

Write the following code in the controller class. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace DatabaseFirst.Controllers { public class EmployeeController : Controller { EmployeeDBContext db = new EmployeeDBContext(); public ActionResult Index() { return View(db.Employees.ToList()); } } } [/csharp]

shape Step-5

Right Click on the controller method and then click on the Add View as shown in the below figure.  

shape Step-6

Then the following window will appear. Give the values as marked in the below figure and click on Add.

shape Step-7

Then, the following code will be generated in the Index.cshtml(newly created view).
    [html][/html]