From 60cc487298c84540c062a120a003dd04d615d47d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Chalet?= Date: Mon, 26 Feb 2024 12:36:08 +0100 Subject: [PATCH] Replace the local CreateTransaction() functions in the EF 6 stores by an internal CreateTransaction() helper --- .../OpenIddictEntityFrameworkHelpers.cs | 25 ++++++++++++++ ...enIddictEntityFrameworkApplicationStore.cs | 16 +-------- ...IddictEntityFrameworkAuthorizationStore.cs | 33 ++----------------- .../OpenIddictEntityFrameworkTokenStore.cs | 18 +--------- 4 files changed, 29 insertions(+), 63 deletions(-) diff --git a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs index cd9695f9..f52903eb 100644 --- a/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs +++ b/src/OpenIddict.EntityFramework/OpenIddictEntityFrameworkHelpers.cs @@ -9,6 +9,7 @@ using System.Runtime.CompilerServices; using Microsoft.Extensions.DependencyInjection; using OpenIddict.EntityFramework; using OpenIddict.EntityFramework.Models; +using OpenIddict.Extensions; namespace System.Data.Entity; @@ -86,4 +87,28 @@ public static class OpenIddictEntityFrameworkHelpers } } } + + /// + /// Tries to create a new with the specified . + /// + /// The Entity Framework context. + /// The desired level of isolation. + /// The if it could be created, otherwise. + internal static DbContextTransaction? CreateTransaction(this DbContext context, IsolationLevel level) + { + if (context is null) + { + throw new ArgumentNullException(nameof(context)); + } + + try + { + return context.Database.BeginTransaction(level); + } + + catch (Exception exception) when (!OpenIddictHelpers.IsFatal(exception)) + { + return null; + } + } } diff --git a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs index 941d2b7d..392ef6e5 100644 --- a/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs +++ b/src/OpenIddict.EntityFramework/Stores/OpenIddictEntityFrameworkApplicationStore.cs @@ -18,7 +18,6 @@ using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using OpenIddict.EntityFramework.Models; -using OpenIddict.Extensions; using static OpenIddict.Abstractions.OpenIddictExceptions; namespace OpenIddict.EntityFramework; @@ -133,19 +132,6 @@ public class OpenIddictEntityFrameworkApplicationStore> ListAuthorizationsAsync() => (from authorization in Authorizations.Include(authorization => authorization.Tokens) where authorization.Application!.Id!.Equals(application.Id) @@ -160,7 +146,7 @@ public class OpenIddictEntityFrameworkApplicationStore> ListTokensAsync() => (from token in Tokens where token.Authorization!.Id!.Equals(authorization.Id) @@ -151,7 +138,7 @@ public class OpenIddictEntityFrameworkAuthorizationStore