Files
deepdrft/DeepDrftData/Data/DeepDrftContextFactory.cs
T

32 lines
1.2 KiB
C#

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using NetBlocks.Utilities.Environment;
namespace DeepDrftData.Data;
public class DeepDrftContextFactory : IDesignTimeDbContextFactory<DeepDrftContext>
{
public DeepDrftContext CreateDbContext(string[] args)
{
var path = CredentialTools.ResolvePath("connections", "environment/connections.json");
string? connectionString = null;
if (File.Exists(path))
{
using var doc = System.Text.Json.JsonDocument.Parse(File.ReadAllText(path));
connectionString = doc.RootElement
.GetProperty("ConnectionStrings")
.GetProperty("DefaultConnection")
.GetString();
}
// Fall back to a design-time dummy — the bundle only needs the provider/schema,
// not a live connection. This removes the requirement to write a dummy file in CI.
connectionString ??= "Host=localhost;Database=deepdrft-design-time;Username=dummy";
var optionsBuilder = new DbContextOptionsBuilder<DeepDrftContext>();
optionsBuilder.UseNpgsql(connectionString);
return new DeepDrftContext(optionsBuilder.Options);
}
}