Merge branch 'factory-fix' — DbContextFactory loads connections.json, no env var required

This commit is contained in:
Daniel Harvey
2026-05-18 22:39:32 -04:00
2 changed files with 20 additions and 10 deletions
@@ -53,9 +53,9 @@ public class TrackConfiguration : BaseEntityConfiguration<TrackEntity>
builder.Property(e => e.CreatedByUserId)
.HasColumnName("created_by_user_id");
// Explicit index on is_deleted so soft-delete global query filters are
// not full table scans. base.Configure may or may not add this depending
// on the BlazorBlocks.Data version; declaring it here guarantees it.
// Names the is_deleted index explicitly. BaseEntityConfiguration.Configure already
// calls HasIndex(e => e.IsDeleted); this adds HasDatabaseName so EF always uses
// "IX_track_is_deleted" regardless of auto-naming conventions.
builder.HasIndex(e => e.IsDeleted).HasDatabaseName("IX_track_is_deleted");
}
}
+17 -7
View File
@@ -7,16 +7,26 @@ public class DeepDrftContextFactory : IDesignTimeDbContextFactory<DeepDrftContex
{
public DeepDrftContext CreateDbContext(string[] args)
{
// For 'dotnet ef' commands, set ConnectionStrings__DefaultConnection in your environment when
// you need to actually hit the database (e.g. `dotnet ef database update`). For model-only
// operations like `migrations add`, the placeholder below is sufficient — EF never connects.
// Example: export ConnectionStrings__DefaultConnection="Host=localhost;Port=5433;Database=postgres;Username=postgres;Password=yourpassword"
var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection")
?? "Host=localhost;Port=5433;Database=postgres;Username=postgres;Password=placeholder";
// Load the real connection string from environment/connections.json — the same
// file DeepDrftWeb's Program.cs loads via CredentialTools. When EF tools run with
// --startup-project DeepDrftWeb, the working directory resolves there, so this
// relative path works without any env var configuration.
const string relPath = "environment/connections.json";
if (!File.Exists(relPath))
throw new FileNotFoundException(
$"'{relPath}' not found. Run EF commands with --startup-project DeepDrftWeb " +
$"from the solution root (current dir: {Directory.GetCurrentDirectory()}).", relPath);
using var doc = System.Text.Json.JsonDocument.Parse(File.ReadAllText(relPath));
var connectionString = doc.RootElement
.GetProperty("ConnectionStrings")
.GetProperty("DefaultConnection")
.GetString()
?? throw new InvalidOperationException(
"ConnectionStrings:DefaultConnection not found in environment/connections.json");
var optionsBuilder = new DbContextOptionsBuilder<DeepDrftContext>();
optionsBuilder.UseNpgsql(connectionString);
return new DeepDrftContext(optionsBuilder.Options);
}
}