There is no native support for multiple result sets in Entity Framework.
If you're using EF6, you can get the
ObjectContext
and manually translate the results, as described in
this SO answer[
^]:
using (TableDBContext context = new TableDBContext())
{
var objectContext = ((IObjectContextAdapter)context).ObjectContext;
using (var command = context.Database.Connection.CreateCommand())
{
command.CommandType = System.Data.CommandType.StoredProcedure;
command.CommandText = "dbo.usp_Retrieve";
command.Parameters.AddWithValue("@Id", id);
command.Parameters.AddWithValue("@Loc", loc);
context.Connection.Open();
using (var reader = command.ExecuteReader())
{
var firstResultSet = objectContext.Translate<RetrieveDE1>(reader, "Set1", MergeOption.AppendOnly).ToList();
reader.NextResult();
var secondResultSet = objectContext.Translate<RetrieveDE2>(reader, "Set2", MergeOption.AppendOnly).ToList();
return new { firstResultSet, secondResultSet };
}
}
}
EF Core doesn't have that option, and doesn't support materializing objects which aren't part of the model. If you're using a
"query type" / "keyless entity type"[
^], you may be able to use something like
this code[
^] to materialize the results. Otherwise, you'll have to load them manually.