Refactor database connection handling and update Docker Compose environment variables #5

This commit is contained in:
Marek Lesko
2025-07-28 10:05:41 +00:00
parent 75e43c7473
commit c8fde6f44c
3 changed files with 19 additions and 18 deletions

View File

@@ -39,8 +39,13 @@ namespace Api
});
builder.Services.AddControllers();
// Add DbContext with SQL Server
// Allow connection string to be set via environment variable (e.g., in Docker)
var envConnectionString = Environment.GetEnvironmentVariable("DB_CONNECTION_STRING");
var connectionString = !string.IsNullOrWhiteSpace(envConnectionString)
? envConnectionString
: builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<AppDbContext>(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));
options.UseSqlServer(connectionString));
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();