Added dbcontext factory

This commit is contained in:
2026-08-15 19:18:17 +02:00
parent 7324268993
commit df7ba866f5
3 changed files with 38 additions and 0 deletions
@@ -0,0 +1,22 @@
namespace PostFundManagement.Infrastructure.Database;
public sealed class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext>
{
public ApplicationDbContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddUserSecrets<IInfrastructure>(optional: true)
.AddEnvironmentVariables()
.Build();
var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>();
var connectionString = configuration.GetConnectionString("PfmDatabase")
?? throw new InvalidOperationException("Connection string 'PfmDatabase' was not found in configuration.");
optionsBuilder.UseNpgsql(connectionString, npgsqlOptions => npgsqlOptions.MigrationsAssembly(typeof(ApplicationDbContext).Assembly.FullName));
return new ApplicationDbContext(optionsBuilder.Options);
}
}