fix(data): load connection string from connections.json; correct HasIndex comment
This commit is contained in:
@@ -53,9 +53,9 @@ public class TrackConfiguration : BaseEntityConfiguration<TrackEntity>
|
|||||||
builder.Property(e => e.CreatedByUserId)
|
builder.Property(e => e.CreatedByUserId)
|
||||||
.HasColumnName("created_by_user_id");
|
.HasColumnName("created_by_user_id");
|
||||||
|
|
||||||
// Explicit index on is_deleted so soft-delete global query filters are
|
// Names the is_deleted index explicitly. BaseEntityConfiguration.Configure already
|
||||||
// not full table scans. base.Configure may or may not add this depending
|
// calls HasIndex(e => e.IsDeleted); this adds HasDatabaseName so EF always uses
|
||||||
// on the BlazorBlocks.Data version; declaring it here guarantees it.
|
// "IX_track_is_deleted" regardless of auto-naming conventions.
|
||||||
builder.HasIndex(e => e.IsDeleted).HasDatabaseName("IX_track_is_deleted");
|
builder.HasIndex(e => e.IsDeleted).HasDatabaseName("IX_track_is_deleted");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,16 +7,26 @@ public class DeepDrftContextFactory : IDesignTimeDbContextFactory<DeepDrftContex
|
|||||||
{
|
{
|
||||||
public DeepDrftContext CreateDbContext(string[] args)
|
public DeepDrftContext CreateDbContext(string[] args)
|
||||||
{
|
{
|
||||||
// For 'dotnet ef' commands, set ConnectionStrings__DefaultConnection in your environment when
|
// Load the real connection string from environment/connections.json — the same
|
||||||
// you need to actually hit the database (e.g. `dotnet ef database update`). For model-only
|
// file DeepDrftWeb's Program.cs loads via CredentialTools. When EF tools run with
|
||||||
// operations like `migrations add`, the placeholder below is sufficient — EF never connects.
|
// --startup-project DeepDrftWeb, the working directory resolves there, so this
|
||||||
// Example: export ConnectionStrings__DefaultConnection="Host=localhost;Port=5433;Database=postgres;Username=postgres;Password=yourpassword"
|
// relative path works without any env var configuration.
|
||||||
var connectionString = Environment.GetEnvironmentVariable("ConnectionStrings__DefaultConnection")
|
const string relPath = "environment/connections.json";
|
||||||
?? "Host=localhost;Port=5433;Database=postgres;Username=postgres;Password=placeholder";
|
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>();
|
var optionsBuilder = new DbContextOptionsBuilder<DeepDrftContext>();
|
||||||
optionsBuilder.UseNpgsql(connectionString);
|
optionsBuilder.UseNpgsql(connectionString);
|
||||||
|
|
||||||
return new DeepDrftContext(optionsBuilder.Options);
|
return new DeepDrftContext(optionsBuilder.Options);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user