ASP.Net MVC, EntityFramework

EntityFramework in C# – Part2 : Calling MS SQL StoredProcedure

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.

StoredProcedure for EF in C#

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 —

EF - Add Function Import

Click Add Function Import and then…

EntityFramework with StoredProcedure - Function Import

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

ImportantDBEntities dbEntities = new ImportantDBEntities();
            
            //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

Standard

Leave a Reply

Your email address will not be published. Required fields are marked *