Skip to content

Commit dc402f2

Browse files
Kamyab7jasontaylordev
authored andcommitted
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.
1 parent 517083e commit dc402f2

File tree

2 files changed

+19
-11
lines changed

2 files changed

+19
-11
lines changed

src/Infrastructure/Data/ApplicationDbContextInitialiser.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,23 @@ namespace CleanArchitecture.Infrastructure.Data;
1111

1212
public static class InitialiserExtensions
1313
{
14+
public static void AddAsyncSeeding(this DbContextOptionsBuilder builder, IServiceProvider serviceProvider)
15+
{
16+
builder.UseAsyncSeeding(async (context, _, ct) =>
17+
{
18+
var initialiser = serviceProvider.GetRequiredService<ApplicationDbContextInitialiser>();
19+
20+
await initialiser.SeedAsync();
21+
});
22+
}
23+
1424
public static async Task InitialiseDatabaseAsync(this WebApplication app)
1525
{
1626
using var scope = app.Services.CreateScope();
1727

1828
var initialiser = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitialiser>();
1929

2030
await initialiser.InitialiseAsync();
21-
22-
await initialiser.SeedAsync();
2331
}
2432
}
2533

src/Infrastructure/DependencyInjection.cs

+9-9
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,22 @@ public static void AddInfrastructureServices(this IHostApplicationBuilder builde
2525
{
2626
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
2727
#if (UsePostgreSQL)
28-
options.UseNpgsql(connectionString);
28+
options.UseNpgsql(connectionString).AddAsyncSeeding(sp);
2929
#elif (UseSqlite)
30-
options.UseSqlite(connectionString);
30+
options.UseSqlite(connectionString).AddAsyncSeeding(sp);
3131
#else
32-
options.UseSqlServer(connectionString);
32+
options.UseSqlServer(connectionString).AddAsyncSeeding(sp);
3333
#endif
34-
});
35-
34+
});
35+
3636
#if (UseAspire)
37-
#if (UsePostgreSQL)
37+
#if (UsePostgreSQL)
3838
builder.EnrichNpgsqlDbContext<ApplicationDbContext>();
39-
#elif (UseSqlServer)
39+
#elif (UseSqlServer)
4040
builder.EnrichSqlServerDbContext<ApplicationDbContext>();
41-
#endif
4241
#endif
43-
42+
#endif
43+
4444
builder.Services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());
4545

4646
builder.Services.AddScoped<ApplicationDbContextInitialiser>();

0 commit comments

Comments
 (0)