Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code First call stored procedure to return multiple results

I am using Code First in a project. The project is half done. I need to call a stored procedure to return multiple results (3 selects). My research tells me that it is not supported in Code First. What options do I have? Changing the project to use Model first?

like image 657
Frank Avatar asked Aug 14 '12 21:08

Frank


People also ask

Can stored procedure return multiple result sets?

Most stored procedures return multiple result sets. Such a stored procedure usually includes one or more select statements. The consumer needs to consider this inclusion to handle all the result sets.

How can we return multiple result sets in stored procedure using Entity Framework?

In order to get multiple result sets working we need to drop to the ObjectContext API by using the IObjectContextAdapter interface. Once we have an ObjectContext then we can use the Translate method to translate the results of our stored procedure into entities that can be tracked and used in EF as normal.

Can we use 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 stored procedure return value?

A stored procedure does not have a return value but can optionally take input, output, or input-output parameters. A stored procedure can return output through any output or input-output parameter.


1 Answers

Roman from MSFT answered my question. He blogged about it here: http://romiller.com/2012/08/15/code-first-stored-procedures-with-multiple-results/

I am sure a lot of people are having the same problem. Hope this can help.

like image 79
Frank Avatar answered Sep 28 '22 09:09

Frank