diff --git a/PostFundManagement.Infrastructure/Database/ApplicationDbContextFactory.cs b/PostFundManagement.Infrastructure/Database/ApplicationDbContextFactory.cs index f3f27d7..a11f435 100644 --- a/PostFundManagement.Infrastructure/Database/ApplicationDbContextFactory.cs +++ b/PostFundManagement.Infrastructure/Database/ApplicationDbContextFactory.cs @@ -1,3 +1,5 @@ +using static PostFundManagement.Infrastructure.Extensions.Constants; + namespace PostFundManagement.Infrastructure.Database; public sealed class ApplicationDbContextFactory : IDesignTimeDbContextFactory @@ -12,8 +14,8 @@ public sealed class ApplicationDbContextFactory : IDesignTimeDbContextFactory(); - var connectionString = configuration.GetConnectionString("PfmDatabase") - ?? throw new InvalidOperationException("Connection string 'PfmDatabase' was not found in configuration."); + var connectionString = configuration.GetConnectionString(DatabaseConfigName) + ?? throw new InvalidOperationException($"Connection string '{DatabaseConfigName}' was not found in configuration."); optionsBuilder.UseNpgsql(connectionString, npgsqlOptions => npgsqlOptions.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName)); diff --git a/PostFundManagement.Infrastructure/Extensions/Constants.cs b/PostFundManagement.Infrastructure/Extensions/Constants.cs new file mode 100644 index 0000000..66ac514 --- /dev/null +++ b/PostFundManagement.Infrastructure/Extensions/Constants.cs @@ -0,0 +1,6 @@ +namespace PostFundManagement.Infrastructure.Extensions; + +public static class Constants +{ + public const string DatabaseConfigName = "PfmDatabase"; +} \ No newline at end of file diff --git a/PostFundManagement.Infrastructure/Extensions/Postgres.cs b/PostFundManagement.Infrastructure/Extensions/Postgres.cs new file mode 100644 index 0000000..2e943c1 --- /dev/null +++ b/PostFundManagement.Infrastructure/Extensions/Postgres.cs @@ -0,0 +1,17 @@ +using PostFundManagement.Infrastructure.Database; +using static PostFundManagement.Infrastructure.Extensions.Constants; + +namespace PostFundManagement.Infrastructure.Extensions; + +public static class Postgres +{ + public static IServiceCollection AddApplicationDbContext(this IServiceCollection services, IConfiguration configuration) + { + var connectionString = configuration.GetConnectionString(DatabaseConfigName) + ?? throw new InvalidOperationException($"Connection string '{DatabaseConfigName}' was not found in configuration."); + + services.AddPooledDbContextFactory(builder => builder.UseNpgsql(connectionString)); + + return services; + } +} \ No newline at end of file diff --git a/PostFundManagement.Infrastructure/PostFundManagement.Infrastructure.csproj b/PostFundManagement.Infrastructure/PostFundManagement.Infrastructure.csproj index b0d01a3..7a4e7e4 100644 --- a/PostFundManagement.Infrastructure/PostFundManagement.Infrastructure.csproj +++ b/PostFundManagement.Infrastructure/PostFundManagement.Infrastructure.csproj @@ -27,13 +27,14 @@ - + +