While we use EntiryFramework as ORM , we can use/call MS SQL StoredProcedure in our C# code . EF allows us to import StoredProcedures into our C# app as Functions .
Now I am gonna show , how we can use SP in EF . To know basic EF,please refer to my another article EntityFramework in C# – Part1 : Welcome To EF
For this demo , I have a simple SP which will return the data from a table.
I will fill an ASP.Net Grid with that data , and I am gonna pull the data using EF .
For this, after taking the update of EDMX , I will get the StoredProcedure inside the EDMX , and will add a Function for that SP like below —
Click Add Function Import and then…
Our SP is now ready in our app as a Function and we can just call that function as usual as we do.So my C# code for calling the function is like below :::
//Execute stored procedure as a function
var employees = dbEntities.GetEmployees();
if (employees != null)
{
employeeGrid.DataSource = employees;
employeeGrid.DataBind();
}
So our Grid is ready with data 🙂
Cheers