| | | 1 | | using Microsoft.EntityFrameworkCore; |
| | | 2 | | using Microsoft.EntityFrameworkCore.Design; |
| | | 3 | | using OzonRepositories.Utils; |
| | | 4 | | using Pomelo.EntityFrameworkCore.MySql.Infrastructure; |
| | | 5 | | |
| | | 6 | | namespace OzonRepositories.Context; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Design-time factory used by EF Core tooling (dotnet ef migrations add/update). |
| | | 10 | | /// Connection string here is for local dev only; production uses the DI-configured context. |
| | | 11 | | /// </summary> |
| | | 12 | | public class ApplicationDbContextFactory : IDesignTimeDbContextFactory<ApplicationDbContext> |
| | | 13 | | { |
| | | 14 | | public ApplicationDbContext CreateDbContext(string[] args) |
| | | 15 | | { |
| | 0 | 16 | | var optionsBuilder = new DbContextOptionsBuilder<ApplicationDbContext>(); |
| | | 17 | | |
| | | 18 | | // Connection string for EF tooling only (migrations). Never used at runtime. |
| | | 19 | | // Set via environment variable before running "dotnet ef migrations add/update": |
| | | 20 | | // export EF_DESIGN_CONNECTION="Server=...;Database=...;User=...;Password=...;" |
| | 0 | 21 | | var connectionString = |
| | 0 | 22 | | Environment.GetEnvironmentVariable("EF_DESIGN_CONNECTION") |
| | 0 | 23 | | ?? throw new InvalidOperationException( |
| | 0 | 24 | | "EF_DESIGN_CONNECTION environment variable is not set. " + |
| | 0 | 25 | | "Set it to a valid MySQL connection string before running EF tooling."); |
| | | 26 | | |
| | 0 | 27 | | var serverVersion = St2MariaDb.ServerVersion; |
| | 0 | 28 | | optionsBuilder.UseMySql(connectionString, serverVersion); |
| | | 29 | | |
| | 0 | 30 | | return new ApplicationDbContext(optionsBuilder.Options); |
| | | 31 | | } |
| | | 32 | | } |