aboutsummaryrefslogtreecommitdiff
path: root/src/Costasdev.Busurbano.ServiceViewer/AppDbContextDesignTimeFactory.cs
diff options
context:
space:
mode:
authorAriel Costas Guerrero <ariel@costas.dev>2025-10-21 15:34:24 +0200
committerAriel Costas Guerrero <ariel@costas.dev>2025-12-12 10:24:53 +0100
commit661cccc2da9a6c32b7b56c60313787282a9084ea (patch)
tree8176720aa99b80281a8351ae74170238c50b59cc /src/Costasdev.Busurbano.ServiceViewer/AppDbContextDesignTimeFactory.cs
parented023a4b5ee257c0c367357b6d83f9778e2cf536 (diff)
Begin implementing
Diffstat (limited to 'src/Costasdev.Busurbano.ServiceViewer/AppDbContextDesignTimeFactory.cs')
-rw-r--r--src/Costasdev.Busurbano.ServiceViewer/AppDbContextDesignTimeFactory.cs41
1 files changed, 41 insertions, 0 deletions
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<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.UseMySQL(
+ connectionString,
+ options => options.MigrationsAssembly(typeof(AppDbContext).Assembly.FullName)
+ );
+
+ return new AppDbContext(builder.Options);
+ }
+}