From 9ee5fe53abe51f567edc6078021fd0655fffdfa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Sat, 3 Feb 2024 17:46:17 +0100 Subject: [PATCH] Update the EF Core stores to resolve the navigation properties via the change tracker when possible --- Directory.Build.targets | 1 + ...ctEntityFrameworkCoreAuthorizationStore.cs | 23 ++++++++-- ...OpenIddictEntityFrameworkCoreTokenStore.cs | 46 +++++++++++++++---- 3 files changed, 55 insertions(+), 15 deletions(-) diff --git a/Directory.Build.targets b/Directory.Build.targets index 51c5ac51..91bb05f5 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -80,6 +80,7 @@ Condition=" ('$(TargetFrameworkIdentifier)' == '.NETCoreApp' And $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '3.0'))) Or ('$(TargetFrameworkIdentifier)' == '.NETStandard' And $([MSBuild]::VersionGreaterThanOrEquals($(TargetFrameworkVersion), '2.1'))) "> $(DefineConstants);SUPPORTS_BCL_ASYNC_ENUMERABLE + $(DefineConstants);SUPPORTS_DBSET_VALUETASK_FINDASYNC application.Id!.Equals(key), cancellationToken) ?? + authorization.Application = GetTrackedEntity() ?? await QueryAsync() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0244)); + + TApplication? GetTrackedEntity() => + (from entry in Context.ChangeTracker.Entries() + where entry.Entity.Id is TKey identifier && identifier.Equals(key) + select entry.Entity).FirstOrDefault(); + + Task QueryAsync() => + (from application in Applications.AsTracking() + where application.Id!.Equals(key) + select application).FirstOrDefaultAsync(cancellationToken); +#endif } else diff --git a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs index cf5920e8..4700ba07 100644 --- a/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs +++ b/src/OpenIddict.EntityFrameworkCore/Stores/OpenIddictEntityFrameworkCoreTokenStore.cs @@ -802,14 +802,27 @@ public class OpenIddictEntityFrameworkCoreTokenStore application.Id!.Equals(key), cancellationToken) ?? + token.Application = GetTrackedEntity() ?? await QueryAsync() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0250)); + + TApplication? GetTrackedEntity() => + (from entry in Context.ChangeTracker.Entries() + where entry.Entity.Id is TKey identifier && identifier.Equals(key) + select entry.Entity).FirstOrDefault(); + + Task QueryAsync() => + (from application in Applications.AsTracking() + where application.Id!.Equals(key) + select application).FirstOrDefaultAsync(cancellationToken); +#endif } else @@ -840,14 +853,27 @@ public class OpenIddictEntityFrameworkCoreTokenStore authorization.Id!.Equals(key), cancellationToken) ?? + token.Authorization = GetTrackedEntity() ?? await QueryAsync() ?? throw new InvalidOperationException(SR.GetResourceString(SR.ID0251)); + + TAuthorization? GetTrackedEntity() => + (from entry in Context.ChangeTracker.Entries() + where entry.Entity.Id is TKey identifier && identifier.Equals(key) + select entry.Entity).FirstOrDefault(); + + Task QueryAsync() => + (from authorization in Authorizations.AsTracking() + where authorization.Id!.Equals(key) + select authorization).FirstOrDefaultAsync(cancellationToken); +#endif } else