In this tutorial i am going to explain about Insert Update Delete(crud operations) in MVC 4 using Stored Procedure. In my previous tutorial i have explained about CRUD operations using Entity Framework Database First Approach and MVC 4, CRUD operations using Entity Framework Code First Approach and MVC 4, ASP.NET MVC client side validation using data annotations, How to create custom validation attribute in mvc 4 and Create primary key,composite key in Entity framework | Use of key attribute in ASP.Net MVC.
Now we will see the implementation of Insert Update Delete(crud operations) in MVC 4 using Stored Procedure.
Open your sql server and create table called Employee and create procedures for insert,update and delete. Below is the complete sql script.
CREATE TABLE dbo.Employee
EmployeeId INT IDENTITY PRIMARY KEY,
CREATE PROC dbo.AddEditEmployee
UPDATE dbo.Employee SET EmployeeName=@EmployeeName,EmployeeAge=@EmployeeAge,
Designation=@Designation WHERE EmployeeId=@EmployeeId
INSERT INTO dbo.Employee(EmployeeName,EmployeeAge,Designation)
SELECT @EmployeeId as EmployeeId
CREATE PROC dbo.GetAllEmployees
CREATE PROC dbo.DeleteEmployeeById
DELETE FROM dbo.Employee WHERE EmployeeId=@EmployeeId
CREATE PROC dbo.GetEmployeeById
FROM dbo.Employee WHERE EmployeeId=@EmployeeId
Now create new mvc project in visual studio. For creating new project refer the below this link. Now right click on project select Add and ADO.Net Entity Model. And then choose EF Designer from database option for Choose model contents and then click Next. Refer below image for reference.
If the next dialog it will ask for connection details. Enter your connection string details and choose your database.
Test you connection and then proceed to next step. In the next step it will list down all the database objects such as tables, procedures and functions etc..
Choose your table, here Employee table i am choosing and then select procedures for insert,update and delete operations. And also check the Import selected stored procedures and functions into the entity model checkbox. Refer above image.
Now if you double click on the Employee.edmx file you can see the entity diagram of employee. Now right click on the diagram and choose Stored Procedure Mapping.
It will give you the option to map Insert Update Delete(crud operations) procedures and we can map the output of procedures. Please refer below screenshot.
Now right click the controllers folder add a new controller with option MVC Controller with views, using Entity Framework. And then choose model and data context classes and then click ok. If you need step by step explanation on how to add controllers refer this link.
Now if you run the program you can see the output of how to do Insert Update Delete(crud operations) in MVC 4 using Stored Procedure.
Hope you learned how to do Insert Update Delete(crud operations) in MVC 4 using Stored Procedure. Do you like this tutorial? Help us to improve. Please post your comments and feedback below.