diff options
| author | Ariel Costas Guerrero <ariel@costas.dev> | 2025-12-29 00:41:52 +0100 |
|---|---|---|
| committer | Ariel Costas Guerrero <ariel@costas.dev> | 2025-12-29 00:41:52 +0100 |
| commit | a304c24b32c0327436bbd8c2853e60668e161b42 (patch) | |
| tree | 08f65c05daca134cf4d2e4f779bd15d98fd66370 /src/Enmarcha.Experimental.ServiceViewer/AppDbContextDesignTimeFactory.cs | |
| parent | 120a3c6bddd0fb8d9fa05df4763596956554c025 (diff) | |
Rename a lot of stuff, add Santiago real time
Diffstat (limited to 'src/Enmarcha.Experimental.ServiceViewer/AppDbContextDesignTimeFactory.cs')
| -rw-r--r-- | src/Enmarcha.Experimental.ServiceViewer/AppDbContextDesignTimeFactory.cs | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/Enmarcha.Experimental.ServiceViewer/AppDbContextDesignTimeFactory.cs b/src/Enmarcha.Experimental.ServiceViewer/AppDbContextDesignTimeFactory.cs new file mode 100644 index 0000000..4dcd320 --- /dev/null +++ b/src/Enmarcha.Experimental.ServiceViewer/AppDbContextDesignTimeFactory.cs @@ -0,0 +1,41 @@ +using Enmarcha.Experimental.ServiceViewer.Data; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; + +namespace Enmarcha.Experimental.ServiceViewer; + +public class AppDbContextDesignTimeFactory : IDesignTimeDbContextFactory<AppDbContext> +{ + 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<AppDbContext>(); + 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.UseNpgsql( + connectionString, + options => options.UseNetTopologySuite() + ); + + return new AppDbContext(builder.Options); + } +} |
