ASP.NET MVC CRUD Operation Using Entity Framework Without Writing Single Code

Here, we are going to discuss how to perform CRUD operations in ASP.NET MVC using Entity Framework. In this modern world, no one is ready to sit and write code for a long time. For that, we can find many predefined features or third party and customizing tools.
  • Visual Studio
  • SQL Management Studio
  • Basics of C# 
  • Basics of SQL Server 
  • Basics of ASP.NET MVC
  • Basics of Entity Framework 
 Article Flow
  • Create Table in SQL Server
  • Create ASP.NET MVC Empty Project
  • Install Entity Framework from NuGet
  • Implement "Database First" approach with Entity Framework
  • Implement scaffolding with Entity Framework
Create Table in SQL Server 
First, we will create a table in SQL Server to perform the CRUD operations in ASP.NET MVC. I have designed the below table.


Execute the below query to create a table with the above design.
  1. CREATE TABLE [dbo].[Employee](  
  2. [ID] [bigint] IDENTITY(1,1) NOT NULL,  
  3. [Name] [nvarchar](maxNULL,  
  4. [Designation] [nvarchar](200) NULL,  
  5. [Location] [nvarchar](200) NULL,  
  7. (  
  8.    [ID] ASC  
  9. ))  
And, insert a few dummy records to view in our project UI. For that, execute the below query.
  1. GO  
  2. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location]) VALUES (1, N'Gnanavel Sekar', N'Software Engineer', N'Chennai')  
  3. GO  
  4. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location]) VALUES (2, N'Subash', N'Software Engineer', N'Coimbatore')  
  5. GO  
  6. INSERT [dbo].[Employee] ([ID], [Name], [Designation], [Location]) VALUES (3, N'Robert', N'Application Developer', N'Banglore')  
  7. GO  
Once you execute the above query, you will get the employee record as below.
Create ASP.NET MVC Empty Project
To create ASP.NET MVC empty project, follow the below steps one by one. Here, I have used Visual Studio 2013.
Step 1 Select Language and Name Your Project

Select New Project -> Visual C# -> Web -> ASP.NET Web Application and enter your application name. Here, I named it  "MVC-CRUDOperation". Now, click OK.

Select Empty MVC project and click OK to create the project.
Step 3
Once you click OK, the project will be created with the basic architecture of MVC.
Install Entity Framework From NuGet
To install Entity Framework from NuGet, right click on References and select Manage NuGet Packages.
And now, search EntityFramework online to install and add the reference to your project.
After installing the EntityFramework from NuGet, it will be added as a reference to our project.
Implement Database First Approach with Entity Framework
Before implementing the database first approach, we should know what the database first approach is. 
Database First Approach

It creates model codes such as classes, properties, and  DbContext etc. from the database in the project and all classes become the links between the database and MVC Controller. It always deals with the existing database. Now, we will move to the steps to implement the database first approach.
Here, I am going to create the .edmx file under the models folder. So, right click on Models folder and select Add->New Item.
Step 2

Select Data->ADO.NET Entity Data Model-> name your edmx file (here, I mentioned as EntityModel.edmx), and click Add. 
Step 3
Here, we have an existing database, so we move on to the database first approach. Select "Generate from database," this caption name will be different on each version of Visual Studio. Now, you are seeing it in Visual Studio 2013.

If you are working with a higher version than Visual Studio 2013, follow the below screen.
Step 4 Configure the connection with sql server
To create connection, click New Connection and select Microsoft SQL Server ->Continue.
Step 5
Select Server name-> Use your authentication. Here, I used  Windows authentication. Then, select database and click Test connection -> OK -> OK.
Step 6
In the below image, you can see that the connection was successfully made.
Step 7
Now, select the table(s) which are needed to be configured with our application through Entity Framework and click Finish.
Step 8
Now, you can see in the below image that the EntityModels are configured with Classes, Properties, DataContext and etc. If you have more than one table, you can easily see the tables with relationships in a .exmx diagram. Here, we have one table only, so there is no possibility to see that.
Implement scaffolding with Entity Framework
What is Scaffolding?
This option is enabled from Visual Studio 2013 for MVC and Web-api. Scaffolding generates the code automatically for CRUD operations, here will use the scaffolding MVC with Entity Framework to create Dynamic code for doingcreate, read, update, and delete. We will see one by one in the following steps for better understanding.
Step 1 Create Controller 
Right click on Controllers folder under your project-> select Add-> Controller .
Step 2

To implement scaffolding Select "MVC5 Controller with views, using Entity Framework" and click add
Step 3
Name the Controller->Select the Models->Select Data context->and click add
Once you click the Add button, it will start the Scaffolding process 
In the below image you can see that the UI for CRUD and Controller are created with codes  
Step 4 Configure RouteConfig.cs
Here we need to mention the Homepage, that means we need to mention which controller and which action needs to be executed first. By default it will be in Home and Index, here we created the controller in the name of CRUD so in controller I mentioned the name "CRUD".
  1. namespace MVC_CRUDOperation {  
  2.     public class RouteConfig {  
  3.         public static void RegisterRoutes(RouteCollection routes) {  
  4.             routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  
  5.             routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new {  
  6.                 controller = "CRUD", action = "Index", id = UrlParameter.Optional  
  7.             });  
  8.         }  
  9.     }  
  10. }  
Now Run your Application
Now Create New Record, here I am adding new employee as follows
Now you can see the below image (list of employee) with new inserted record "Ramar"

Now try to edit the record as follows, here I changed the location from Banglore to Chennai
Now see the updated employee list after clicking save and the below image represents that Robert's location has been changed
 Now we try to delete the record which belongs to "Subash"
After Deleting the subash record you can see the employee list as below
Now shall we see the particular employee detail by clicking Details link
And now view the Database table
In this you learned to perform CRUD (Create,Read,Update and Delete) operations using MVC scaffolding with Entity Framework. 
  • Create a table in Sql Server
  • Create Asp.Net MVC Empty Project
  • Install Entity Framework From Nuget
  • Implement Database First Appraoch with Entity Framework
  • Implement scaffolding with Entity Framework
I didn't attach the code due to its scaffolding so you can easily easily do these all within a few minutes. I hope it was helpful. Your feedback and comments are always welcome.