using Bit.Core.Enums; using Bit.Core.Repositories; using Bit.Infrastructure.EntityFramework.Repositories; using LinqToDB.EntityFrameworkCore; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Bit.Infrastructure.EntityFramework; public static class EntityFrameworkServiceCollectionExtensions { public static void AddEFRepositories(this IServiceCollection services, bool selfHosted, string connectionString, SupportedDatabaseProviders provider) { if (string.IsNullOrWhiteSpace(connectionString)) { throw new Exception($"Database provider type {provider} was selected but no connection string was found."); } LinqToDBForEFTools.Initialize(); services.AddAutoMapper(typeof(UserRepository)); services.AddDbContext(options => { if (provider == SupportedDatabaseProviders.Postgres) { options.UseNpgsql(connectionString, b => b.MigrationsAssembly("PostgresMigrations")); // Handle NpgSql Legacy Support for `timestamp without timezone` issue AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); } else if (provider == SupportedDatabaseProviders.MySql) { options.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString), b => b.MigrationsAssembly("MySqlMigrations")); } }); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); services.AddSingleton(); if (selfHosted) { services.AddSingleton(); } } }