Skip to content

Commit 21db14d

Browse files
committed
feat(persistence): add provider-specific Postgres DbContexts and initial EF migrations (API/Auth)
1 parent 1c6be44 commit 21db14d

16 files changed

Lines changed: 2181 additions & 30 deletions

src/TaskManagement.Api/Infrastructure/Persistence/Configuration/DatabaseConfiguration.cs

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,34 @@ public static IServiceCollection AddDatabaseConfiguration(this IServiceCollectio
1111
var sqlServerConnectionString = configuration.GetConnectionString("TaskManagementDbConnection");
1212
var postgresConnectionString = configuration.GetConnectionString("TaskManagementDbConnectionPostgres");
1313

14-
services.AddDbContext<TaskManagementDbContext>(options =>
14+
if (IsPostgres(databaseProvider))
1515
{
16-
if (IsPostgres(databaseProvider))
16+
if (string.IsNullOrWhiteSpace(postgresConnectionString))
1717
{
18-
if (string.IsNullOrWhiteSpace(postgresConnectionString))
19-
{
20-
throw new InvalidOperationException("Connection string 'TaskManagementDbConnectionPostgres' not found.");
21-
}
18+
throw new InvalidOperationException("Connection string 'TaskManagementDbConnectionPostgres' not found.");
19+
}
2220

21+
services.AddDbContext<TaskManagementDbContextPostgres>(options =>
22+
{
2323
options.UseNpgsql(postgresConnectionString,
2424
npgsqlOptions => npgsqlOptions.EnableRetryOnFailure(
2525
maxRetryCount: 5,
2626
maxRetryDelay: TimeSpan.FromSeconds(10),
2727
errorCodesToAdd: null));
28-
return;
29-
}
28+
});
3029

31-
if (string.IsNullOrWhiteSpace(sqlServerConnectionString))
32-
{
33-
throw new InvalidOperationException("Connection string 'TaskManagementDbConnection' not found.");
34-
}
30+
services.AddScoped<TaskManagementDbContext>(sp =>
31+
sp.GetRequiredService<TaskManagementDbContextPostgres>());
32+
return services;
33+
}
3534

35+
if (string.IsNullOrWhiteSpace(sqlServerConnectionString))
36+
{
37+
throw new InvalidOperationException("Connection string 'TaskManagementDbConnection' not found.");
38+
}
39+
40+
services.AddDbContext<TaskManagementDbContext>(options =>
41+
{
3642
options.UseSqlServer(sqlServerConnectionString,
3743
sqlServerOptions => sqlServerOptions.EnableRetryOnFailure(
3844
maxRetryCount: 5,
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.EntityFrameworkCore.Design;
3+
4+
namespace TaskManagement.Api.Infrastructure.Persistence.DesignTime
5+
{
6+
public sealed class TaskManagementDbContextPostgresFactory : IDesignTimeDbContextFactory<TaskManagementDbContextPostgres>
7+
{
8+
public TaskManagementDbContextPostgres CreateDbContext(string[] args)
9+
{
10+
var connectionString =
11+
Environment.GetEnvironmentVariable("ConnectionStrings__TaskManagementDbConnectionPostgres")
12+
?? "Host=localhost;Port=5432;Database=TaskManagementDb;Username=postgres;Password=postgres";
13+
14+
var optionsBuilder = new DbContextOptionsBuilder<TaskManagementDbContextPostgres>();
15+
optionsBuilder.UseNpgsql(connectionString);
16+
17+
return new TaskManagementDbContextPostgres(optionsBuilder.Options);
18+
}
19+
}
20+
}

src/TaskManagement.Api/Infrastructure/Persistence/Migrations/Postgres/20260226095157_InitialPostgres.Designer.cs

Lines changed: 251 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)