Skip to content

Commit

Permalink
feat: Update database seeding implementation to use EF Core 9 `UseAsy…
Browse files Browse the repository at this point in the history
…ncSeeding` 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.
  • Loading branch information
Kamyab7 committed Jan 14, 2025
1 parent 8f5a963 commit fa6853d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
12 changes: 10 additions & 2 deletions src/Infrastructure/Data/ApplicationDbContextInitialiser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,23 @@ 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<ApplicationDbContextInitialiser>();

await initialiser.SeedAsync();
});
}

public static async Task InitialiseDatabaseAsync(this WebApplication app)
{
using var scope = app.Services.CreateScope();

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

await initialiser.InitialiseAsync();

await initialiser.SeedAsync();
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/Infrastructure/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ public static void AddInfrastructureServices(this IHostApplicationBuilder builde
{
options.AddInterceptors(sp.GetServices<ISaveChangesInterceptor>());
#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<ApplicationDbContext>();
#elif (UseSqlServer)
#elif (UseSqlServer)
builder.EnrichSqlServerDbContext<ApplicationDbContext>();
#endif
#endif

#endif

builder.Services.AddScoped<IApplicationDbContext>(provider => provider.GetRequiredService<ApplicationDbContext>());

builder.Services.AddScoped<ApplicationDbContextInitialiser>();
Expand Down

0 comments on commit fa6853d

Please sign in to comment.