diff --git a/src/Infrastructure/Data/ApplicationDbContextInitialiser.cs b/src/Infrastructure/Data/ApplicationDbContextInitialiser.cs index 7a0d3abc5..75e01bc2c 100644 --- a/src/Infrastructure/Data/ApplicationDbContextInitialiser.cs +++ b/src/Infrastructure/Data/ApplicationDbContextInitialiser.cs @@ -11,6 +11,16 @@ namespace CleanArchitecture.Infrastructure.Data; public static class InitialiserExtensions { + public static void AddAsyncSeeding(this DbContextOptionsBuilder builder, IServiceProvider serviceProvider) + { + builder.UseAsyncSeeding(async (context, _, ct) => + { + var initialiser = serviceProvider.GetRequiredService(); + + await initialiser.SeedAsync(); + }); + } + public static async Task InitialiseDatabaseAsync(this WebApplication app) { using var scope = app.Services.CreateScope(); @@ -18,8 +28,6 @@ public static async Task InitialiseDatabaseAsync(this WebApplication app) var initialiser = scope.ServiceProvider.GetRequiredService(); await initialiser.InitialiseAsync(); - - await initialiser.SeedAsync(); } } diff --git a/src/Infrastructure/DependencyInjection.cs b/src/Infrastructure/DependencyInjection.cs index 3bf140824..ecd89851a 100644 --- a/src/Infrastructure/DependencyInjection.cs +++ b/src/Infrastructure/DependencyInjection.cs @@ -25,22 +25,22 @@ public static void AddInfrastructureServices(this IHostApplicationBuilder builde { options.AddInterceptors(sp.GetServices()); #if (UsePostgreSQL) - options.UseNpgsql(connectionString); + options.UseNpgsql(connectionString).AddAsyncSeeding(sp); #elif (UseSqlite) - options.UseSqlite(connectionString); + options.UseSqlite(connectionString).AddAsyncSeeding(sp); #else - options.UseSqlServer(connectionString); + options.UseSqlServer(connectionString).AddAsyncSeeding(sp); #endif - }); - + }); + #if (UseAspire) - #if (UsePostgreSQL) +#if (UsePostgreSQL) builder.EnrichNpgsqlDbContext(); - #elif (UseSqlServer) +#elif (UseSqlServer) builder.EnrichSqlServerDbContext(); - #endif #endif - +#endif + builder.Services.AddScoped(provider => provider.GetRequiredService()); builder.Services.AddScoped();