using Microsoft.EntityFrameworkCore;
using WebApiTemplate.Data.Entities;
namespace WebApiTemplate.Data.Sql;
public class DatabaseContext : DbContext
{
public DatabaseContext(DbContextOptions<DatabaseContext> options) : base(options)
{
}
public required DbSet<Books> Books { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (optionsBuilder.IsConfigured) return;
var host = Environment.GetEnvironmentVariable("DB_HOST");
var port = Environment.GetEnvironmentVariable("DB_PORT");
var user = Environment.GetEnvironmentVariable("DB_USER");
var password = Environment.GetEnvironmentVariable("DB_PASSWORD");
var database = Environment.GetEnvironmentVariable("DB_DATABASE");
var connectionString = $"Host={host};Port={port};Username={user};Password={password};Database={database}";
optionsBuilder.UseNpgsql(connectionString);
}
}