From fa6853dfd09ef1050e1d98d2e745092f8d5bde9e Mon Sep 17 00:00:00 2001 From: Kamyab7 Date: Tue, 14 Jan 2025 18:47:56 +0330 Subject: [PATCH] feat: Update database seeding implementation to use EF Core 9 `UseAsyncSeeding` method - Introduced `AddAsyncSeeding` extension method to integrate the new `UseAsyncSeeding` approach introduced in EF Core 9. - Refactored `ApplicationDbContextInitialiser` seeding logic to align with the new asynchronous seeding mechanism. - Updated `AddDbContext` configuration in `AddInfrastructureServices` to use the `AddAsyncSeeding` method for all database providers (`PostgreSQL`, `SQLite`, `SQL Server`). - Separated database initialization (`InitialiseAsync`) and seeding (`SeedAsync`) into distinct operations for better alignment with the new seeding approach. - Maintained compatibility with dependency injection for interceptors and service provider usage. This change modernizes the database seeding process to leverage the improved EF Core 9 capabilities, ensuring better integration with async workflows. --- .../Data/ApplicationDbContextInitialiser.cs | 12 ++++++++++-- src/Infrastructure/DependencyInjection.cs | 18 +++++++++--------- 2 files changed, 19 insertions(+), 11 deletions(-) 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();