ASP.Net, ASP.Net MVC, C#, 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;

So our Grid is ready with data 🙂



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s