Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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.
Posted 2-Jan-14 23:27pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Check this sample application:
 
Entity Framework for Beginners[^]
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

check this link might be helpful for you:
 
how to execute stored procedures in sqlquery in the dbcontext
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

actually Entity Framework does not support SP
but if you insist to use it , i answered this question in link below
 
How to call Stored Procedure in Entity Framework 6 (Code-First)?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Comments
apurba001 at 3-Jan-14 10:05am
   
Thanks Jameel for your reply. I followed the link you have posted but it does not given the way how to execute the stored procedure from code behind.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Refer - Code First Insert/Update/Delete Stored Procedures[^].
 
It describes how to configure your Code First model to use stored procedures and pass parameters to them.
Comments
Member 4750507 at 7-Mar-14 18:46pm
   
That article is only for insert/update/delete stored procedures. What about just general stored procedures that do a bunch of things and return a recordset? That aren't related to insert/update/delete?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 379
1 Nirav Prabtani 266
2 Abhinav S 210
3 PIEBALDconsult 160
4 Dave Kreskowiak 155
0 OriginalGriff 7,545
1 Sergey Alexandrovich Kryukov 6,757
2 Maciej Los 3,909
3 Peter Leow 3,693
4 CHill60 2,712


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 7 May 2014
Copyright © CodeProject, 1999-2014
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