diff --git a/src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProvider.cs b/src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProvider.cs index a1ca637f..d6d91c7c 100644 --- a/src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProvider.cs +++ b/src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProvider.cs @@ -69,7 +69,7 @@ protected internal SqlServerTestDbContextProvider(SqlServerTestDbContextProvider ArgumentNullException.ThrowIfNull(options); Schema = options.Schema ?? throw new ArgumentException($"The '{nameof(options.Schema)}' cannot be null.", nameof(options)); - _sharedTablesIsolationLevel = ValidateIsolationLevel(options); + _sharedTablesIsolationLevel = ValidateIsolationLevel(options.SharedTablesIsolationLevel); _isUsingSharedTables = options.IsUsingSharedTables; _masterConnection = options.MasterConnection ?? throw new ArgumentException($"The '{nameof(options.MasterConnection)}' cannot be null.", nameof(options)); _masterDbContextOptions = options.MasterDbContextOptions ?? throw new ArgumentException($"The '{nameof(options.MasterDbContextOptions)}' cannot be null.", nameof(options)); @@ -81,18 +81,18 @@ protected internal SqlServerTestDbContextProvider(SqlServerTestDbContextProvider _contextFactory = options.ContextFactory; } - private static IsolationLevel ValidateIsolationLevel(SqlServerTestDbContextProviderOptions options) + private static IsolationLevel ValidateIsolationLevel(IsolationLevel? isolationLevel) { - if (!options.SharedTablesIsolationLevel.HasValue) + if (!isolationLevel.HasValue) return IsolationLevel.ReadCommitted; - if (Enum.IsDefined(options.SharedTablesIsolationLevel.Value)) - throw new ArgumentException($"The provided isolation level '{options.SharedTablesIsolationLevel}' is invalid."); + if (Enum.IsDefined(isolationLevel.Value)) + throw new ArgumentException($"The provided isolation level '{isolationLevel}' is invalid.", nameof(isolationLevel)); - if (options.SharedTablesIsolationLevel < IsolationLevel.ReadCommitted) - throw new ArgumentException($"The isolation level '{options.SharedTablesIsolationLevel}' cannot be less than '{nameof(IsolationLevel.ReadCommitted)}'."); + if (isolationLevel < IsolationLevel.ReadCommitted) + throw new ArgumentException($"The isolation level '{isolationLevel}' cannot be less than '{nameof(IsolationLevel.ReadCommitted)}'.", nameof(isolationLevel)); - return options.SharedTablesIsolationLevel.Value; + return isolationLevel.Value; } /// diff --git a/src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProviderBuilder.cs b/src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProviderBuilder.cs index 7cab00c9..29856d79 100644 --- a/src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProviderBuilder.cs +++ b/src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProviderBuilder.cs @@ -1,3 +1,4 @@ +using System.Data; using System.Data.Common; using System.Globalization; using Microsoft.Data.SqlClient; @@ -25,6 +26,7 @@ public class SqlServerTestDbContextProviderBuilder : TestDbContextProviderBui private bool _useThinktectureSqlServerMigrationsSqlGenerator = true; private string? _sharedTablesSchema; + private IsolationLevel? _sharedTablesIsolationLevel; private Func, IDbDefaultSchema, T?>? _contextFactory; private Func, SqlServerTestDbContextProvider?>? _providerFactory; @@ -42,6 +44,19 @@ public SqlServerTestDbContextProviderBuilder(string connectionString, bool useSh _ctxInitializations = new List>(); } + /// + /// Specifies the isolation level to use with shared tables. + /// Default is . + /// + /// Isolation level to use. + /// Current builder for chaining + public SqlServerTestDbContextProviderBuilder UseSharedTablesIsolationLevel(IsolationLevel sharedTablesIsolationLevel) + { + _sharedTablesIsolationLevel = sharedTablesIsolationLevel; + + return this; + } + /// /// Specifies the migration strategy to use. /// Default is . @@ -319,7 +334,8 @@ public SqlServerTestDbContextProvider Build() { IsUsingSharedTables = _useSharedTables, ContextFactory = _contextFactory, - ExecutedCommands = state.CommandCapturingInterceptor?.Commands + ExecutedCommands = state.CommandCapturingInterceptor?.Commands, + SharedTablesIsolationLevel = _sharedTablesIsolationLevel }; return _providerFactory?.Invoke(options) ?? new SqlServerTestDbContextProvider(options);