Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stored procedure in Entity Framework database first approach

I am doing transition for a project from Webforms to MVC application using Entity Framework database first approach and have database ready along with all stored procedures in place.

I successfully created an .edmx file and was able to use my stored procedures and it worked great when there was any insert or update operation to perform. But the real problem occurred when I was using select query in one of my stored procedures.

For example, there is an Employee table which has following columns:

EmpId, FirstName, LastName, Age, Salary

I have a stored procedure GetAllEmpDetails which has following select query.

Select 
    EmpId, (FirstName + ' ' + LastName) as FullName, Salary 
from 
    Employee

Now when I am trying to bind the result of this stored procedure with the Employee class which has 5 properties as per the table structure, then I am getting an error that value for Age property is expected but it is not available in the resultset.

I know there is no FullName property as well, so my question is how to solve this problem with the model class generated (as in this case Employee) so that it can tackle these dynamism?

like image 388
Deepak Tekchandani Avatar asked May 06 '17 13:05

Deepak Tekchandani


People also ask

How can we use stored procedure in Entity Framework database first approach?

Open the SchoolModel. Store node and then open the Stored Procedures node. Then right-click the GetCourses stored procedure and select Add Function Import. In the Add Function Import dialog box, under Returns a Collection Of select Entities, and then select Course as the entity type returned.

Can we create stored procedure in code first approach?

To use a Stored Procedure with the Code First model, we need to override the OnModelCreating method of DBContext and add the following code to map the Stored Procedure. The MapToStoreProcedures method has two overloaded methods, one method is without a parameter.

Can we use stored procedure in Entity Framework?

The Entity Framework has the capability of importing a Stored Procedure as a function. We can also map the result of the function back to any entity type or complex type.


1 Answers

How to map a stored procedure in EF?

Since you are doing Database First Approach and you have an EDMX file, let EF generate the class of the stored procedure result for you. You may have many stored procedures and you want to avoid creating the classes manually: After all that is the whole point of using an ORM tool. Also some of your stored procedures may have parameters. Doing it the way below will handle all that for you. It is actually pretty simple.

To get EF to do this for you, follow the steps to below:

  1. Double click your EDMX file
  2. Choose Update Model from Database

You will see the dialog similar to below:

enter image description here

  1. Make sure you have checked the boxes as shown.

That will add the stored procedure and you will see it in your model browser as shown below:

enter image description here

  1. If you want to change the class name auto-generated by EF then do so. I strongly suggest you do this and give your class a meaningful names that follow .NET naming conventions. The convention I follow is remove any verbs from the stored procedure name and append the word result to the end. So you will end up with name as shown below:

enter image description here

  1. Press OK

Some Notes

This is much better than writing the classes manually in case your stored procedure name, or the parameters it needs, or the result it returns changes. This approach will work for user defined functions as well.

A Gotcha

There will be times when the stored procedure will not appear in the selection in the wizard dialog, that is because of this. Simply add this to the beginning of your stored procedure:

SET FMTONLY OFF -- REMEMBER to remove it once the wizard is done.
like image 100
CodingYoshi Avatar answered Oct 25 '22 23:10

CodingYoshi