//----------------------------------------------------------------------- // // Copyright (c) lanedirt. All rights reserved. // Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information. // //----------------------------------------------------------------------- namespace AliasServerDb; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; /// /// The PostgreSQL DbContext factory. /// public class PostgresqlDbContextFactory : IAliasServerDbContextFactory { private readonly IConfiguration _configuration; /// /// Initializes a new instance of the class. /// /// The configuration. public PostgresqlDbContextFactory(IConfiguration configuration) { _configuration = configuration; } /// public AliasServerDbContext CreateDbContext() { var optionsBuilder = new DbContextOptionsBuilder(); ConfigureDbContextOptions(optionsBuilder); return new AliasServerDbContext(optionsBuilder.Options); } /// public Task CreateDbContextAsync(CancellationToken cancellationToken = default) { return Task.FromResult(CreateDbContext()); } /// public void ConfigureDbContextOptions(DbContextOptionsBuilder optionsBuilder) { AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); // Check environment variable first. var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__AliasServerDbContext"); // If no environment variable, fall back to configuration. if (string.IsNullOrEmpty(connectionString)) { connectionString = _configuration.GetConnectionString("AliasServerDbContext"); } optionsBuilder .UseNpgsql(connectionString, options => options.CommandTimeout(60)) .UseLazyLoadingProxies(); } }