Click here to Skip to main content
13,045,564 members (69,885 online)

How to call a Stored Procedure in Code First Entity Framework 6

apurba001 asked:

Open original thread
I am very new to Entity Framework 6 and i want to implement Stored Procedure in my project. I searched in google deeply but couldn't find any solution in which i can follow the implementation. I have a stored procedure like below.

ALTER PROCEDURE [dbo].[insert_department]
@Name [varchar](100)
INSERT [dbo].[Departments]([Name])
VALUES (@Name)
DECLARE @DeptId int
SELECT @DeptId = [DeptId]
FROM [dbo].[Departments]
WHERE @@ROWCOUNT > 0 AND [DeptId] = scope_identity()
SELECT t0.[DeptId]
FROM [dbo].[Departments] AS t0
WHERE @@ROWCOUNT > 0 AND t0.[DeptId] = @DeptId

Department Class

public class Department
    public int DepartmentId { get; set; }       
    public string Name { get; set; }
.MapToStoredProcedures(s => 
s.Update(u => u.HasName("modify_department") 
               .Parameter(b => b.Department, "department_id") 
               .Parameter(b => b.Name, "department_name")) 
 .Delete(d => d.HasName("delete_department") 
               .Parameter(b => b.DepartmentId, "department_id")) 
 .Insert(i => i.HasName("insert_department") 
               .Parameter(b => b.Name, "department_name")));
protected void btnSave_Click(object sender, EventArgs e)
          string department = txtDepartment.text.trim();
         // Here i want to call the stored procedure to insert values

My problem is how do i call the stored procedure and pass parameters into it. Please help me out. Thanks in advance.
Tags: C# (C#4.0), ASP.NET, entity-framework


When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100