w4c-workflows-api/Data/WorkflowsDbContextFactory.cs

32 lines
1.1 KiB
C#
Raw Permalink Normal View History

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;
namespace w4c_workflows.Data;
/// <summary>
/// Design-time factory so `dotnet ef` builds the context directly from
/// appsettings.json instead of running the web host (Program.cs applies
/// migrations at startup, which must not execute during tooling runs).
/// </summary>
public class WorkflowsDbContextFactory : IDesignTimeDbContextFactory<WorkflowsDbContext>
{
public WorkflowsDbContext CreateDbContext(string[] args)
{
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddEnvironmentVariables()
.Build();
var connectionString = configuration.GetConnectionString("DefaultConnection")
?? "Host=localhost;Port=5432;Database=w4c;Username=w4c;Password=w4c";
var options = new DbContextOptionsBuilder<WorkflowsDbContext>()
.UseNpgsql(connectionString)
.Options;
return new WorkflowsDbContext(options);
}
}