Skip to content

Commit 0246ea2

Browse files
committed
Builder method for setting the SharedTablesIsolationLevel
1 parent 896abbf commit 0246ea2

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProvider.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected internal SqlServerTestDbContextProvider(SqlServerTestDbContextProvider
6969
ArgumentNullException.ThrowIfNull(options);
7070

7171
Schema = options.Schema ?? throw new ArgumentException($"The '{nameof(options.Schema)}' cannot be null.", nameof(options));
72-
_sharedTablesIsolationLevel = ValidateIsolationLevel(options);
72+
_sharedTablesIsolationLevel = ValidateIsolationLevel(options.SharedTablesIsolationLevel);
7373
_isUsingSharedTables = options.IsUsingSharedTables;
7474
_masterConnection = options.MasterConnection ?? throw new ArgumentException($"The '{nameof(options.MasterConnection)}' cannot be null.", nameof(options));
7575
_masterDbContextOptions = options.MasterDbContextOptions ?? throw new ArgumentException($"The '{nameof(options.MasterDbContextOptions)}' cannot be null.", nameof(options));
@@ -81,18 +81,18 @@ protected internal SqlServerTestDbContextProvider(SqlServerTestDbContextProvider
8181
_contextFactory = options.ContextFactory;
8282
}
8383

84-
private static IsolationLevel ValidateIsolationLevel(SqlServerTestDbContextProviderOptions<T> options)
84+
private static IsolationLevel ValidateIsolationLevel(IsolationLevel? isolationLevel)
8585
{
86-
if (!options.SharedTablesIsolationLevel.HasValue)
86+
if (!isolationLevel.HasValue)
8787
return IsolationLevel.ReadCommitted;
8888

89-
if (Enum.IsDefined(options.SharedTablesIsolationLevel.Value))
90-
throw new ArgumentException($"The provided isolation level '{options.SharedTablesIsolationLevel}' is invalid.");
89+
if (Enum.IsDefined(isolationLevel.Value))
90+
throw new ArgumentException($"The provided isolation level '{isolationLevel}' is invalid.", nameof(isolationLevel));
9191

92-
if (options.SharedTablesIsolationLevel < IsolationLevel.ReadCommitted)
93-
throw new ArgumentException($"The isolation level '{options.SharedTablesIsolationLevel}' cannot be less than '{nameof(IsolationLevel.ReadCommitted)}'.");
92+
if (isolationLevel < IsolationLevel.ReadCommitted)
93+
throw new ArgumentException($"The isolation level '{isolationLevel}' cannot be less than '{nameof(IsolationLevel.ReadCommitted)}'.", nameof(isolationLevel));
9494

95-
return options.SharedTablesIsolationLevel.Value;
95+
return isolationLevel.Value;
9696
}
9797

9898
/// <inheritdoc />

src/Thinktecture.EntityFrameworkCore.SqlServer.Testing/EntityFrameworkCore/Testing/SqlServerTestDbContextProviderBuilder.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Data;
12
using System.Data.Common;
23
using System.Globalization;
34
using Microsoft.Data.SqlClient;
@@ -25,6 +26,7 @@ public class SqlServerTestDbContextProviderBuilder<T> : TestDbContextProviderBui
2526

2627
private bool _useThinktectureSqlServerMigrationsSqlGenerator = true;
2728
private string? _sharedTablesSchema;
29+
private IsolationLevel? _sharedTablesIsolationLevel;
2830
private Func<DbContextOptions<T>, IDbDefaultSchema, T?>? _contextFactory;
2931
private Func<SqlServerTestDbContextProviderOptions<T>, SqlServerTestDbContextProvider<T>?>? _providerFactory;
3032

@@ -42,6 +44,19 @@ public SqlServerTestDbContextProviderBuilder(string connectionString, bool useSh
4244
_ctxInitializations = new List<Action<T>>();
4345
}
4446

47+
/// <summary>
48+
/// Specifies the isolation level to use with shared tables.
49+
/// Default is <see cref="IsolationLevel.ReadCommitted"/>.
50+
/// </summary>
51+
/// <param name="sharedTablesIsolationLevel">Isolation level to use.</param>
52+
/// <returns>Current builder for chaining</returns>
53+
public SqlServerTestDbContextProviderBuilder<T> UseSharedTablesIsolationLevel(IsolationLevel sharedTablesIsolationLevel)
54+
{
55+
_sharedTablesIsolationLevel = sharedTablesIsolationLevel;
56+
57+
return this;
58+
}
59+
4560
/// <summary>
4661
/// Specifies the migration strategy to use.
4762
/// Default is <see cref="IMigrationExecutionStrategy.Migrations"/>.
@@ -319,7 +334,8 @@ public SqlServerTestDbContextProvider<T> Build()
319334
{
320335
IsUsingSharedTables = _useSharedTables,
321336
ContextFactory = _contextFactory,
322-
ExecutedCommands = state.CommandCapturingInterceptor?.Commands
337+
ExecutedCommands = state.CommandCapturingInterceptor?.Commands,
338+
SharedTablesIsolationLevel = _sharedTablesIsolationLevel
323339
};
324340

325341
return _providerFactory?.Invoke(options) ?? new SqlServerTestDbContextProvider<T>(options);

0 commit comments

Comments
 (0)