This commit is contained in:
Ccjcarkbla
2026-02-23 13:14:57 -05:00
parent 1a8d8fef70
commit 718ac72c57
8 changed files with 295 additions and 0 deletions
+32
View File
@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
namespace maintenance.Data;
public sealed class AppDbContextFactory : IDesignTimeDbContextFactory<AppDbContext>
{
public AppDbContext CreateDbContext(string[] args)
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development";
var configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true)
.AddJsonFile($"appsettings.{environment}.json", optional: true)
.AddJsonFile("FutureSecrets.json", optional: true)
.AddEnvironmentVariables()
.Build();
var connectionString = configuration.GetConnectionString("PrimaryDb");
if (string.IsNullOrWhiteSpace(connectionString))
{
throw new InvalidOperationException(
"Missing connection string for design-time DbContext: ConnectionStrings:PrimaryDb");
}
var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
optionsBuilder.UseSqlServer(connectionString);
return new AppDbContext(optionsBuilder.Options);
}
}
+61
View File
@@ -0,0 +1,61 @@
using maintenance.Data.Entities;
using Microsoft.EntityFrameworkCore;
namespace maintenance.Data.Seed;
public static class TemplateSeeder
{
public static async Task SeedAsync(AppDbContext dbContext, CancellationToken cancellationToken = default)
{
var defaults = new[]
{
new TemplateEntity
{
Id = Guid.NewGuid(),
Name = "generic",
Title = "System Error",
Body = "A system error has occurred. Please contact MAX Support.",
SupportContactBlock = null,
LastUpdatedBy = "system-seed",
LastUpdatedAtUtc = DateTime.UtcNow
},
new TemplateEntity
{
Id = Guid.NewGuid(),
Name = "unavailable",
Title = "Application Unavailable",
Body = "MAX applications may be down for scheduled maintenance 2AM-8AM Sunday morning.\nIf you are receiving this message at any other time, please contact MAX Support.",
SupportContactBlock = null,
LastUpdatedBy = "system-seed",
LastUpdatedAtUtc = DateTime.UtcNow
},
new TemplateEntity
{
Id = Guid.NewGuid(),
Name = "template",
Title = "Error Name",
Body = "An error has occurred. Please contact MAX Support for assistance.",
SupportContactBlock = null,
LastUpdatedBy = "system-seed",
LastUpdatedAtUtc = DateTime.UtcNow
}
};
var existingNames = await dbContext.Templates
.AsNoTracking()
.Select(x => x.Name)
.ToListAsync(cancellationToken);
var missing = defaults
.Where(x => !existingNames.Contains(x.Name, StringComparer.OrdinalIgnoreCase))
.ToList();
if (missing.Count == 0)
{
return;
}
await dbContext.Templates.AddRangeAsync(missing, cancellationToken);
await dbContext.SaveChangesAsync(cancellationToken);
}
}