using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Design; using Microsoft.Extensions.Configuration; using Nocr.Users.Persistence; namespace Nocr.Users.Migrator; public class DesignTimeTextMatcherContextFactory : IDesignTimeDbContextFactory { private const string EnvironmentVariableKey = "DOTNET_ENVIRONMENT"; public UsersContext CreateDbContext(string[] args) { var optionsBuilder = new DbContextOptionsBuilder(); var configuration = new ConfigurationBuilder() .SetBasePath(AppDomain.CurrentDomain.BaseDirectory) .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var environment = configuration.GetValue(EnvironmentVariableKey); if (string.IsNullOrWhiteSpace(environment)) { throw new InvalidOperationException($"Отсутствует переменная окружения {EnvironmentVariableKey}"); } var connectionString = configuration.GetConnectionString(environment); if (string.IsNullOrWhiteSpace(connectionString)) throw new InvalidOperationException($"ConnectionString for environment `{environment}` not found"); optionsBuilder.UseMySql(connectionString, new MariaDbServerVersion(MariaDbServerVersion.LatestSupportedServerVersion), builder => builder.MigrationsAssembly("Nocr.Users.Migrator")); return new UsersContext(optionsBuilder.Options); } }