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 Started

Create a table and its stored procedure

Customer 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)
       -- SET NOCOUNT ON added to prevent extra result sets from
       -- interfering with SELECT statements.
    -- Insert statements for procedure here
       INSERT INTO [dbo].[Customer]
           (@Name ,

Getting stared with visual studio 2012

Note: After creating customer table and its procedure open visual studio and create a simple MVC application.

After creating MVC application go to Model folder in solution explorer and add an ADO.NET Entity Data Model (Read this article to add an entity framework

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.        

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

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


After doing above steps write the code in HomeController


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

public ActionResult Index()
     return View();
public ActionResult Index(Customer customer)
     ViewBag.Message = "Record Inserted!";
     return View();

@model WebGridMvc.Models.Customer
@using (Html.BeginForm("Index", "Home", FormMethod.Post, new { @id = "frmSaveData"})) {
            <td>@Html.Label("Customer Name")</td>
            <td>@Html.TextBoxFor(m => m.Name)</td>
            <td>@Html.Label("Customer Address")</td>
            <td>@Html.TextBoxFor(m => m.Address)</td>
            <td>@Html.Label("Customer Contact")</td>
            <td>@Html.TextBoxFor(m => m.ContactNo)</td>
                <div style="color: red; font-size: larger;">@ViewBag.Message</div>             </td>
                <input type="submit" /></td>

  Modified On Sep-18-2014 01:24:17 PM

Leave Comment