using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Hangfire; using Hangfire.MemoryStorage; using Hangfire.PostgreSql; namespace BTCPayServer.Data { public enum DatabaseType { Sqlite, Postgres } public class ApplicationDbContextFactory { string _ConnectionString; DatabaseType _Type; public ApplicationDbContextFactory(DatabaseType type, string connectionString) { _ConnectionString = connectionString ?? throw new ArgumentNullException(nameof(connectionString)); _Type = type; } public ApplicationDbContext CreateContext() { var builder = new DbContextOptionsBuilder(); ConfigureBuilder(builder); return new ApplicationDbContext(builder.Options); } public void ConfigureBuilder(DbContextOptionsBuilder builder) { if (_Type == DatabaseType.Sqlite) builder.UseSqlite(_ConnectionString); else if (_Type == DatabaseType.Postgres) builder.UseNpgsql(_ConnectionString); } public void ConfigureHangfireBuilder(IGlobalConfiguration builder) { builder.UseMemoryStorage(); //We always use memory storage because of incompatibilities with the latest postgres in 2.1 //if (_Type == DatabaseType.Sqlite) // builder.UseMemoryStorage(); //Sqlite provider does not support multiple workers //else if (_Type == DatabaseType.Postgres) // builder.UsePostgreSqlStorage(_ConnectionString); } } }