In Entity Framework 6 (EF6) on .NET Framework, reading
multiple result sets from a stored procedure asynchronously is not directly supported out of the box. EF6 supports
MultipleResults only via synchronous APIs. However, you can do it asynchronously by dropping down to
DbContext.Database.SqlQuery and DbDataReader, using
ExecuteReaderAsync.
Goal
Call a stored procedure that returns multiple result sets, e.g.:
CREATE PROCEDURE GetData
AS
BEGIN
SELECT * FROM Products;
SELECT * FROM Categories;
END
Solution: Use DbCommand + ExecuteReaderAsync
Here’s a full example using ADO.NET with EF6 DbContext to read multiple result sets asynchronously.
Step-by-Step Example
public async Task<(List<Product> Products, List<Category> Categories)> GetMultipleResultsAsync()
{
using (var context = new YourDbContext())
{
var conn = context.Database.Connection;
if (conn.State != ConnectionState.Open)
await ((DbConnection)conn).OpenAsync();
using (var command = conn.CreateCommand())
{
command.CommandText = "GetProductAndCategory";
command.CommandType = CommandType.StoredProcedure;
using (var reader = await ((DbCommand)command).ExecuteReaderAsync())
{
var products = ((IObjectContextAdapter)context)
.ObjectContext
.Translate<Product>(reader)
.ToList();
await reader.NextResultAsync();
var categories = ((IObjectContextAdapter)context)
.ObjectContext
.Translate<Category>(reader)
.ToList();
return (products, categories);
}
}
}
}
Calling the Method
In your controller or service:
public async Task<ActionResult> Index()
{
var result = await GetMultipleResultsAsync();
var products = result.Products;
var categories = result.Categories;
return View(new MyViewModel
{
Products = products,
Categories = categories
});
}
Explanation
Part
Purpose
DbContext.Database.Connection
Gets the underlying ADO.NET connection.
ExecuteReaderAsync()
Executes the command and returns a DbDataReader.
Translate<T>()
Maps raw data to your EF entity.
NextResultAsync()
Advances to the next result set.
Notes
Translate<T>() is only available in EF6 through ObjectContext.
This pattern requires manual mapping, but it’s the only async-compatible way in EF6 to handle multiple result sets.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
In Entity Framework 6 (EF6) on .NET Framework, reading multiple result sets from a stored procedure asynchronously is not directly supported out of the box. EF6 supports
MultipleResultsonly via synchronous APIs. However, you can do it asynchronously by dropping down toDbContext.Database.SqlQueryandDbDataReader, usingExecuteReaderAsync.Goal
Call a stored procedure that returns multiple result sets, e.g.:
Solution: Use
DbCommand+ExecuteReaderAsyncHere’s a full example using ADO.NET with EF6 DbContext to read multiple result sets asynchronously.
Step-by-Step Example
Calling the Method
In your controller or service:
Explanation
DbContext.Database.ConnectionExecuteReaderAsync()DbDataReader.Translate<T>()NextResultAsync()Notes
Translate<T>()is only available in EF6 throughObjectContext.