---
title: "Insert value using entity framework in MVC4 with stored procedure mapping"  
description: "In this blog I'm trying to explain the concept of how to insert a value in a table using stored procedure mapping in entity framework.  Getting Starte"  
author: "Vijay Shukla"  
published: 2013-09-26  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/588/insert-value-using-entity-framework-in-mvc4-with-stored-procedure-mapping  
category: "asp.net mvc"  
tags: ["asp.net mvc"]  
reading_time: 3 minutes  

---

# Insert value using entity framework in MVC4 with stored procedure mapping

In this blog I'm trying to [explain the concept](https://www.mindstick.com/forum/159605/explain-the-concept-of-unique-key-violation-error) of how to insert a value in a table using [stored procedure](https://www.mindstick.com/articles/803/using-stored-procedure-in-asp-dot-net) mapping in [entity framework](https://www.mindstick.com/articles/1566/crud-operations-using-entity-framework-code-first-approach).\

##### Getting Started

##### Create a table and its stored procedure

[Customer](https://www.mindstick.com/articles/325839/how-to-improve-your-business-level-of-customer-service) Table

```
CREATE TABLE [dbo].[Customer](
       [CustID] [int] IDENTITY(1,1) NOT NULL,       [Name] [varchar](100) NULL,       [Address] [varchar](200) NULL,       [ContactNo] [varchar](20) NULL,)
```

Customer Table Insert Stored Procured

```
CREATE PROCEDURE usp_InsertCustomer
       -- Add the parameters for the stored procedure here       @Name varchar(100),          @Address varchar(200),          @ContactNo varchar(20)ASBEGIN       -- SET NOCOUNT ON added to prevent extra result sets from       -- interfering with SELECT statements.       SET NOCOUNT ON;
    -- Insert statements for procedure here       INSERT INTO [dbo].[Customer]           ([Name]           ,[Address]           ,[ContactNo])
     VALUES           (@Name ,               @Address,               @ContactNo)
END
GO
```

##### Getting stared with visual studio 2012

**Note**: After creating customer table and its procedure open [visual studio](https://www.mindstick.com/articles/12378/visual-studio-for-mac-is-out-of-beta-preview-now-officially-available) and create a simple [MVC application](https://www.mindstick.com/forum/452/do-i-need-to-install-mvc-3-4-on-web-server-to-run-mvc-application).

After creating MVC application go to Model folder in solution explorer and add an ADO.NET [Entity Data Model](https://www.mindstick.com/articles/594/ado-dot-net-entity-data-model-in-wpf) (Read this article to add an entity framework http://www.mindstick.com/Articles/6dfea253-4698-4eb8-ab20-57d2aa61753a/?Fetch%20Data%20from%20Entity%20Framework%20Using%20ASP.NET%20MVC4)

After adding ADO.NET Entity Data Model need to mapping the stored procedure, below the steps of mapping the stored procedure in entity framework: -

1. Right Click on Entity Framework and select the Stored Procedure Mapping.

**\**![Insert value using entity framework in MVC4 with stored procedure mapping](https://www.mindstick.com/blogs/997fccb5-6158-401d-be20-6274fd7a018f/images/80b31299-985b-49eb-a5f7-2de055a561de.png)

2. After selecting Stored Procedure Mapping below screen will be appear and the select the sp_InsertCustomer.

\
![Insert value using entity framework in MVC4 with stored procedure mapping](https://www.mindstick.com/blogs/997fccb5-6158-401d-be20-6274fd7a018f/images/408af481-5573-4d0d-bef4-7ae27ac11ca4.png)\

3. After selecting sp_InsertCustomer stored procedure below screen will be appear. Then Clear and build your solution.

\
![Insert value using entity framework in MVC4 with stored procedure mapping](https://www.mindstick.com/blogs/997fccb5-6158-401d-be20-6274fd7a018f/images/6cf642ce-7523-45ce-8d84-96abf2d3028e.png)

After doing above steps write the code in HomeController

##### HomeController

DEMOEntities demoEntity = new DEMOEntities();//Here i'm create My entity global object

```
public ActionResult Index(){     return View();}
[HttpPost]public ActionResult Index(Customer customer){
     demoEntity.Customers.Add(customer);     demoEntity.SaveChanges();     ViewBag.Message = "Record Inserted!";     return View();
}
```

##### View

```
@model WebGridMvc.Models.Customer@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "frmSaveData"}))
{ <table>        <tr>            <td>@Html.Label("Customer Name")</td>            <td>@Html.TextBoxFor(m => m.Name)</td>        </tr>        <tr>            <td>@Html.Label("Customer Address")</td>            <td>@Html.TextBoxFor(m => m.Address)</td>        </tr>        <tr>            <td>@Html.Label("Customer Contact")</td>            <td>@Html.TextBoxFor(m => m.ContactNo)</td>        </tr>        <tr>            <td>                <div style="color: red; font-size: larger;">@ViewBag.Message</div>
            </td>            <td>                <input type="submit" /></td>        </tr>    </table>
}
```

---

Original Source: https://www.mindstick.com/blog/588/insert-value-using-entity-framework-in-mvc4-with-stored-procedure-mapping

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
