Introduction To ASP.NET MVC With Entity Framework

In this article, I am going to describe to you how to perform basic CRUD operations in an MVC4 application using Entity Framework.

Definition of Entity Framework given by Microsoft:

"The Microsoft ADO.NET Entity Framework is an Object/Relational Mapping (ORM) framework that enables developers to work with relational data as domain-specific objects and eliminates the need for most of the data access plumbing code that developers usually need to write. Using the Entity Framework, developers issue queries using LINQ, retrieve and manipulate data as strongly typed objects. The Entity Framework's ORM implementation provides services like change tracking, identity resolution, lazy loading and query translation so that developers can focus on their application-specific business logic rather than the data access fundamentals."

So let's get started…

Step 1: Create a database named "Asp.NetMVC" and add a table named "Employee" to it.

 

Step 2: Open your latest version of Visual Studio, add an MVC Internet application and name the project as EntityFrameworkDemo as follows:



Step 3: Right-click the project file name, select "Add a new item" and add the ADO .Net Entity Data Model. Follow the procedure in the wizard as shown below and name it as Model1.edmx.

 


Generate a model from the database, select your server and "Asp.NetMVC" database name. The connection string will automatically be added to your Web.Config.

 

Click on Finish button to complete the procedure. The screen appears as follows: 

 

Step 4: Add a new controller to the Controller folder. Right-click the controller folder and add a controller named "Default1". Since we already created our data model, we can choose an option where CRUD actions are created by the chosen Entity Framework data model.

 

Refer the following steps

  • Name your controller as "Default1Controller",
  • From the Scaffolding Options, select "MVC controller with read/write actions and views with the help of Entity Framework".
  • Select the Model class as Employee that lies in our solution.
  • Select the Data Context class as "Entities" that are added to our solution when we tried to add the Entity Framework data model.
  • Select Razor as the rendering engine for views.

Step 5: We can see Default1controller prepared with all the CRUD operation actions as shown below:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Data;  
  4. using System.Data.Entity;  
  5. using System.Linq;  
  6. using System.Web;  
  7. using System.Web.Mvc;  
  8. namespace EntityFrameworkDemo.Controllers  
  9. {  
  10.     public class Default1Controller: Controller  
  11.     {  
  12.         private Entities db = new Entities();  
  13.         //  
  14.         // GET: /Default1/  
  15.         public ActionResult Index()  
  16.         {  
  17.             return View(db.Employees.ToList());  
  18.         }  
  19.         //  
  20.         // GET: /Default1/Details/5  
  21.         public ActionResult Details(int id = 0)  
  22.         {  
  23.             Employee employee = db.Employees.Find(id);  
  24.             if (employee == null)  
  25.             {  
  26.                 return HttpNotFound();  
  27.             }  
  28.             return View(employee);  
  29.         }  
  30.         //  
  31.         // GET: /Default1/Create  
  32.         public ActionResult Create()  
  33.         {  
  34.             return View();  
  35.         }  
  36.         //  
  37.         // POST: /Default1/Create  
  38.         [HttpPost]  
  39.         public ActionResult Create(Employee employee)  
  40.         {  
  41.             if (ModelState.IsValid)  
  42.             {  
  43.                 db.Employees.Add(employee);  
  44.                 db.SaveChanges();  
  45.                 return RedirectToAction("Index");  
  46.             }  
  47.             return View(employee);  
  48.         }  
  49.         //  
  50.         // GET: /Default1/Edit/5  
  51.         public ActionResult Edit(int id = 0)  
  52.         {  
  53.             Employee employee = db.Employees.Find(id);  
  54.             if (employee == null)  
  55.             {  
  56.                 return HttpNotFound();  
  57.             }  
  58.             return View(employee);  
  59.         }  
  60.         //  
  61.         // POST: /Default1/Edit/5  
  62.         [HttpPost]  
  63.         public ActionResult Edit(Employee employee)  
  64.         {  
  65.             if (ModelState.IsValid)  
  66.             {  
  67.                 db.Entry(employee).State = EntityState.Modified;  
  68.                 db.SaveChanges();  
  69.                 return RedirectToAction("Index");  
  70.             }  
  71.             return View(employee);  
  72.         }  
  73.         //  
  74.         // GET: /Default1/Delete/5  
  75.         public ActionResult Delete(int id = 0)  
  76.         {  
  77.             Employee employee = db.Employees.Find(id);  
  78.             if (employee == null)  
  79.             {  
  80.                 return HttpNotFound();  
  81.             }  
  82.             return View(employee);  
  83.         }  
  84.         //  
  85.         // POST: /Default1/Delete/5  
  86.         [HttpPost, ActionName("Delete")]  
  87.         public ActionResult DeleteConfirmed(int id)  
  88.         {  
  89.             Employee employee = db.Employees.Find(id);  
  90.             db.Employees.Remove(employee);  
  91.             db.SaveChanges();  
  92.             return RedirectToAction("Index");  
  93.         }  
  94.         protected override void Dispose(bool disposing)  
  95.         {  
  96.             db.Dispose();  
  97.             base.Dispose(disposing);  
  98.         }  
  99.     }  
  100. }  

Step 6: Open the "App_Start" folder and change the name of the controller from "Home" to "Default1".

 

Step 7: Now press F5 to run the application

Index Page

 
Create Page



Edit Page

 
Delete Page:

 
Hope you liked this article.

Up Next
    Ebook Download
    View all
    Learn
    View all