diff --git a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs
index 92ea38ca..cd9695f9 100644
--- a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs
+++ b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs
@@ -4,6 +4,7 @@
* the license and the contributors participating to this project.
*/
+using System.Data.Entity.Infrastructure;
using System.Runtime.CompilerServices;
using Microsoft.Extensions.DependencyInjection;
using OpenIddict.EntityFramework;
@@ -60,12 +61,12 @@ public static class OpenIddictEntityFrameworkHelpers
}
///
- /// Executes the query and returns the results as a non-streamed async enumeration.
+ /// Executes the query and returns the results as a streamed async enumeration.
///
/// The type of the returned entities.
/// The query source.
/// The that can be used to abort the operation.
- /// The non-streamed async enumeration containing the results.
+ /// The streamed async enumeration containing the results.
internal static IAsyncEnumerable AsAsyncEnumerable(this IQueryable source, CancellationToken cancellationToken)
{
if (source is null)
@@ -77,9 +78,11 @@ public static class OpenIddictEntityFrameworkHelpers
static async IAsyncEnumerable ExecuteAsync(IQueryable source, [EnumeratorCancellation] CancellationToken cancellationToken)
{
- foreach (var element in await source.ToListAsync(cancellationToken))
+ using var enumerator = ((IDbAsyncEnumerable)source).GetAsyncEnumerator();
+
+ while (await enumerator.MoveNextAsync(cancellationToken))
{
- yield return element;
+ yield return enumerator.Current;
}
}
}