From 1ce1ad0df816540df518e622598e1b006bd21f51 Mon Sep 17 00:00:00 2001 From: Kamyab7 Date: Tue, 14 Jan 2025 18:21:39 +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 | 14 +++++++++--- src/Infrastructure/DependencyInjection.cs | 22 ++++++++++--------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/Infrastructure/Data/ApplicationDbContextInitialiser.cs b/src/Infrastructure/Data/ApplicationDbContextInitialiser.cs index 7a0d3abc5..d02681b9b 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(); } } @@ -82,7 +90,7 @@ public async Task TrySeedAsync() await _userManager.CreateAsync(administrator, "Administrator1!"); if (!string.IsNullOrWhiteSpace(administratorRole.Name)) { - await _userManager.AddToRolesAsync(administrator, new [] { administratorRole.Name }); + await _userManager.AddToRolesAsync(administrator, new[] { administratorRole.Name }); } } diff --git a/src/Infrastructure/DependencyInjection.cs b/src/Infrastructure/DependencyInjection.cs index 3bf140824..fe6f399b2 100644 --- a/src/Infrastructure/DependencyInjection.cs +++ b/src/Infrastructure/DependencyInjection.cs @@ -1,5 +1,7 @@ -using CleanArchitecture.Application.Common.Interfaces; +using System; +using CleanArchitecture.Application.Common.Interfaces; using CleanArchitecture.Domain.Constants; +using CleanArchitecture.Domain.Entities; using CleanArchitecture.Infrastructure.Data; using CleanArchitecture.Infrastructure.Data.Interceptors; using CleanArchitecture.Infrastructure.Identity; @@ -25,22 +27,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();