Skip to main content

Development

EF Core: Stored Procedure Multiple Result Sets

Young developers working together, programming.
Stored procedures are powerful tools for running complex database operations efficiently. EF Core execute stored procedure multiple result sets, a technique that can improve performance and simplify data retrieval.

It’s important to handle the results properly when working with stored procedures that return multiple tables. Entity Framework Core allows you to execute stored procedures and process multiple result sets. Still, the logic must be carefully structured to ensure all data is retrieved and mapped correctly.

Learn more about raw SQL and stored procedures in the official EF Core documentation.

In this example, we’ll use a stored procedure that returns multiple result sets representing different report data. We’ll create a generic method to execute the stored procedure and map the results to their corresponding models.

1. Defining the Models

Before executing the stored procedure, we need to define models to hold the results of each result set. These models represent the different types of data the SP will return.

EF Core stored procedure multiple result sets model

Each model corresponds to one of the report tables that the stored procedure will return.

2. Writing the Method to Execute the Stored Procedure

Now, let’s create the ExecuteMultiTableStoredProcAsync method to execute the stored procedure and process the multiple result sets.

Here’s how you can execute the stored procedure and map the result sets to the appropriate models:

a) Define the Method Signature

  • Define a static async method that showcases how EF Core executes stored procedures with multiple result sets and returns a model for each.
  • This method should accept the database context (DbContext), stored procedure name (spName), and any necessary parameters (like currentDate).

EF Core method to execute stored procedure multiple result sets

b) Validate Input Parameters

  • Use ArgumentNullException.ThrowIfNull to ensure that the DbContext and stored procedure name (spName) are not null.

EF Core stored procedure parameter validation

c)  Set Up Database Connection and Command

  • Create a database connection using source.Database.GetDbConnection().
  • Create a command using the connection.CreateCommand() method, and set the command text to the stored procedure name.

EF Core set up connection for stored procedure multiple result sets

d) Add Parameters to the Command

  • Create any necessary parameters for the stored procedure (e.g., @currentDate) using command.CreateParameter().
  • Add the parameter to the command.Parameters collection.

EF Core stored procedure command parameters

e) Open the Database Connection and Initialize the Result Model

  • Open the connection asynchronously using await connection.OpenAsync().
  • Initialize the MultiTableReportModel to hold the result sets.

EF Core open connection and initialize multiple result sets model

f) Execute the Command and Read Result Sets

  • Use await command.ExecuteReaderAsync() to execute the stored procedure and get a DbDataReader [See the official API documentation for DbDataReader here].
  • Read each result set sequentially using the ReadResultSetAsync method.
  • After reading each result set, use await reader.NextResultAsync() to move to the next result set.

EF Core execute stored procedure and read multiple result sets

3. Calling the Stored Procedure

Once the method for executing the stored procedure is set up, you can call it from your application’s service layer or controller to retrieve the data.

Here’s how you can call the ExecuteMultiTableStoredProcAsync method in your code:

EF Core calling stored procedure with multiple result sets

4. Accessing the Result Sets

Now, process each result set one at a time by iterating over the rows of data and mapping them to their respective models.

EF Core access and process multiple result sets

In this blog post, we explored how EF Core execute stored procedure multiple result sets functionality enables efficient handling of complex database operations. The process involves creating a database command, executing the stored procedure, and handling multiple result sets by reading them individually. This approach allows you to map the results to different models, making it easier to process and use the data within your application.

This technique is helpful in scenarios where a single SP returns multiple datasets, like generating complex reports containing multiple data sections.

For more insights into designing efficient stored procedures, check out this guide on Stored Procedure Best Practices in SQL Server.

 

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Ankita Sachdev

Ankita is a Lead Technical Consultant at Perficient with 7+ years of experience in Full-Stack .NET Development. She is a dedicated and skilled Full-Stack .NET Developer with extensive experience designing, developing, and maintaining web applications. With a robust background in both front-end and back-end technologies, Ankita excels at creating seamless and efficient user experiences.

More from this Author

Follow Us