In this article we are concentrating on two ways by which we can call stored procedure using entity framework.

Entity Framework performs below expectation when fetching large data, so often we use stored procedure to fetch data.

In the application where one has used entity framework, one can use entity framework itself to call stored procedure.

DemoLets see what are these two ways:Way 1:This is probably the simplest method to call a stored procedure with minimal effort.We have created a simple stored procedure which fetches the registered user to the application. The stored procedure looks like below:

In the above image above is the stored procedure and below is the result that stored procedure returns.

In the above code, we have created a data context object. We have used ExecuteStoreQuery method of data context to fetch the data. The method accepts a parameter which is the name of the stored procedure to call. We are retuning list of type Register class which maps to the Register table in database whose data we are retreiving using stored procedure.Way 2:In this method, we first add stored procedure to the .edmx file. The function inport for the stored procedure is created, function import creates a function which uses the stored procedure.Step 1: Adding stored procedure to EFOpen the edmx file, right click on it, a dialog box will appear as shown below:

Click on Update Model from Database option which will open another dialog box as shown below:

Select the stored procedure and click the Finish button. This will add the stored procedure to the solution or to the edmx file.

In the above image you could see RegisteredUsers added under Stored Procedures folder.Step 2:In this step we will see how to add funtion import for the stored procedure.Right click on the stored procedure added, this will open a window as shown below:

click on the Add Function Import option, this will open another window as shown below:

The function import name field is pre populated, you can give the function name you want, the stored procedure dropdown has the stored procedure for which you want to add function import.

Select the Entities radio button and select the entity to which you want to map the stored procedure. The stored procedure will return the result of the selected entity type. On clicking ok the funtion import will be created for stored procedure.