MVC CRUD Operations Using Entity Framework


In this article I am going to talk about MVC CRUD Operations Using Entity Framework 6 without writing any code. First,  you should learn about MVC and the basics of Entity Framework.

  • The View is responsible for the look and feel.
  • Model represents the real world object and provides data to the View.
  • The Controller is responsible for taking the end user's request and loading the appropriate Model and View.
  • Download Sample

Building the Sample

Step 1

Create sample employee SQL table:

  1. CREATE TABLE [dbo].[Employee](   
  2.    [EmployeeId] [int] primary key identity(1,1),   
  3.    [FirstName] [nvarchar](50) NULL,   
  4.    [LastName] [nvarchar](50) NULL,   
  5.    [Address1] [nvarchar](50) NULL,   
  6.    [Address2] [nvarchar](50) NULL,   
  7.    [EmailID] [nvarchar](50) NULL,   
  8.    )  

Step 2

Open Visual studio -> Create New Project WebApplication with MVC:

Entity Framework

Step 3

Right Click on the Project and go to Manage NuGet Packages..

Entity Framework

Step 4

Click on the browse button then find entity framework. In this article I have selected framework 6:

Entity Framework

Step 5

Right Click on the project -> go to Add -> New Item - >Select ADO.NET Entity Data Model. Give model name.

Entity Framework

Step 6

Click on the  New Connection -> Select Data Source as Microsoft SQL Server

Entity Framework

Step 7

Choose Server Name & Database Name then click OK

Entity Framework

Step 8

By default connection string will be generated -> Select Table Data

Entity Framework
Entity Framework

Step 9

Add new controller with the name "Employee" 

Entity Framework
Entity Framework

Step 10

Select Model Class and Data Context cClass from combo box. Select Generate View Checkbox and give controller a name.

Entity Framework

Step 11

Change default "Home" controller to 
 "Employees" and hit F5 to run the sample.

  1. public class RouteConfig   
  2.     {   
  3.         public static void RegisterRoutes(RouteCollection routes)   
  4.         {   
  5.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");   
  7.             routes.MapRoute(   
  8.                 name: "Default",   
  9.                 url: "{controller}/{action}/{id}",   
  10.                 defaults: new { controller = "Employees", action = "Index", id = UrlParameter.Optional }   
  11.             );   
  12.         }   
  13.     }   
Entity Framework

Step 12

Without writing a single of code the application is created with CRUD views.

Entity Framework
Entity Framework
Entity Framework