Click here to Skip to main content
11,433,997 members (58,569 online)

apurba doley 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)
AS
BEGIN
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
END

Department Class

public class Department
{
 
    public int DepartmentId { get; set; }       
    public string Name { get; set; }
 
}
 
modelBuilder 
.Entity<Department>() 
.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#4.0, ASP.NET, EntityFramework

Preview



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
Web03 | 2.8.150428.2 | Last Updated 26 Mar 2009
Copyright © CodeProject, 1999-2015
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