From 661cccc2da9a6c32b7b56c60313787282a9084ea Mon Sep 17 00:00:00 2001 From: Ariel Costas Guerrero Date: Tue, 21 Oct 2025 15:34:24 +0200 Subject: Begin implementing --- .../AppDbContextDesignTimeFactory.cs | 41 ++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/Costasdev.Busurbano.ServiceViewer/AppDbContextDesignTimeFactory.cs (limited to 'src/Costasdev.Busurbano.ServiceViewer/AppDbContextDesignTimeFactory.cs') diff --git a/src/Costasdev.Busurbano.ServiceViewer/AppDbContextDesignTimeFactory.cs b/src/Costasdev.Busurbano.ServiceViewer/AppDbContextDesignTimeFactory.cs new file mode 100644 index 0000000..4caaabc --- /dev/null +++ b/src/Costasdev.Busurbano.ServiceViewer/AppDbContextDesignTimeFactory.cs @@ -0,0 +1,41 @@ +using Costasdev.ServiceViewer.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; + +namespace Costasdev.ServiceViewer; + +public class AppDbContextDesignTimeFactory : IDesignTimeDbContextFactory +{ + public AppDbContext CreateDbContext(string[] args) + { + IConfigurationRoot configuration = new ConfigurationBuilder() + .AddJsonFile("appsettings.json", optional: true) + .AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", + optional: true) + .AddUserSecrets(typeof(AppDbContext).Assembly, optional: true) + .AddEnvironmentVariables() + .Build(); + + var builder = new DbContextOptionsBuilder(); + var connectionString = configuration.GetConnectionString("Database"); + if (string.IsNullOrEmpty(connectionString)) + { + throw new InvalidOperationException("Connection string 'Database' not found."); + } + + var loggerFactory = LoggerFactory.Create(lb => + { + lb + .AddConsole() + .SetMinimumLevel(LogLevel.Information); + }); + builder.UseLoggerFactory(loggerFactory); + + builder.UseMySQL( + connectionString, + options => options.MigrationsAssembly(typeof(AppDbContext).Assembly.FullName) + ); + + return new AppDbContext(builder.Options); + } +} -- cgit v1.3