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)
AS
BEGIN
       -- 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 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 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.        


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

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>
}

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

Leave Comment