From b3d21ade2004434ba14189f38f7bc6c8333677f2 Mon Sep 17 00:00:00 2001 From: Robin Sue Date: Thu, 4 Aug 2022 16:59:23 +0200 Subject: [PATCH] Provide a real streamed IAsyncEnumerable implementation for Entity Framework 6.x --- .../OpenIddictEntityFrameworkHelpers.cs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) 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; } } }