diff --git a/test/EFCore.Specification.Tests/SharedStoreFixtureBase.cs b/test/EFCore.Specification.Tests/SharedStoreFixtureBase.cs index 162b259f613..057ac3f9c71 100644 --- a/test/EFCore.Specification.Tests/SharedStoreFixtureBase.cs +++ b/test/EFCore.Specification.Tests/SharedStoreFixtureBase.cs @@ -16,10 +16,17 @@ public abstract class SharedStoreFixtureBase : FixtureBase, IDisposabl where TContext : DbContext { protected virtual Type ContextType { get; } = typeof(TContext); - public IServiceProvider ServiceProvider { get; } + + private IServiceProvider _serviceProvider; + public IServiceProvider ServiceProvider + => _serviceProvider ?? throw new InvalidOperationException($"The {nameof(ServiceProvider)} is only available after {nameof(InitializeAsync)} has executed."); + protected abstract string StoreName { get; } protected abstract ITestStoreFactory TestStoreFactory { get; } - public TestStore TestStore { get; } + + private TestStore _testStore; + public TestStore TestStore + => _testStore ?? throw new InvalidOperationException($"The {nameof(TestStore)} is only available after {nameof(InitializeAsync)} has executed."); protected virtual bool UsePooling => true; @@ -35,9 +42,9 @@ private IDbContextPool ContextPool public ListLoggerFactory ListLoggerFactory => _listLoggerFactory ??= (ListLoggerFactory)ServiceProvider.GetRequiredService(); - protected SharedStoreFixtureBase() + public virtual Task InitializeAsync() { - TestStore = TestStoreFactory.GetOrCreate(StoreName); + _testStore = TestStoreFactory.GetOrCreate(StoreName); var services = AddServices(TestStoreFactory.AddProviderServices(new ServiceCollection())); if (UsePooling) @@ -53,13 +60,10 @@ protected SharedStoreFixtureBase() ServiceLifetime.Singleton); } - ServiceProvider = services.BuildServiceProvider(validateScopes: true); + _serviceProvider = services.BuildServiceProvider(validateScopes: true); TestStore.Initialize(ServiceProvider, CreateContext, c => Seed((TContext)c), c => Clean(c)); - } - public virtual Task InitializeAsync() - { return Task.CompletedTask; } diff --git a/test/EFCore.SqlServer.FunctionalTests/MigrationsInfrastructureSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/MigrationsInfrastructureSqlServerTest.cs index a3c9b45d577..d7de3d560eb 100644 --- a/test/EFCore.SqlServer.FunctionalTests/MigrationsInfrastructureSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/MigrationsInfrastructureSqlServerTest.cs @@ -1399,9 +1399,10 @@ public class MigrationsInfrastructureSqlServerFixture : MigrationsInfrastructure protected override ITestStoreFactory TestStoreFactory => SqlServerTestStoreFactory.Instance; - public MigrationsInfrastructureSqlServerFixture() + public override async Task InitializeAsync() { - ((SqlServerTestStore)TestStore).ExecuteNonQuery( + await base.InitializeAsync(); + await ((SqlServerTestStore)TestStore).ExecuteNonQueryAsync( @"USE master IF EXISTS(select * from sys.databases where name='TransactionSuppressed') DROP DATABASE TransactionSuppressed");