Phase 1 WP-1: EF Core DbContext with Fluent API mappings for all 26 entities

ScadaLinkDbContext with 10 configuration classes (Fluent API only), initial
migration creating 25 tables, environment-aware migration helper (auto-apply
dev, validate-only prod), DesignTimeDbContextFactory, optimistic concurrency
on DeploymentRecord. 20 tests verify schema, CRUD, relationships, cascades.
This commit is contained in:
Joseph Doherty
2026-03-16 19:15:50 -04:00
parent 9bc5a5163f
commit 1996b21961
23 changed files with 4494 additions and 9 deletions

View File

@@ -0,0 +1,40 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.Audit;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class AuditLogEntryConfiguration : IEntityTypeConfiguration<AuditLogEntry>
{
public void Configure(EntityTypeBuilder<AuditLogEntry> builder)
{
builder.HasKey(a => a.Id);
builder.Property(a => a.User)
.IsRequired()
.HasMaxLength(200);
builder.Property(a => a.Action)
.IsRequired()
.HasMaxLength(100);
builder.Property(a => a.EntityType)
.IsRequired()
.HasMaxLength(200);
builder.Property(a => a.EntityId)
.IsRequired()
.HasMaxLength(200);
builder.Property(a => a.EntityName)
.IsRequired()
.HasMaxLength(200);
// Indexes for common query patterns
builder.HasIndex(a => a.Timestamp);
builder.HasIndex(a => a.User);
builder.HasIndex(a => a.EntityType);
builder.HasIndex(a => a.EntityId);
builder.HasIndex(a => a.Action);
}
}

View File

@@ -0,0 +1,63 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.Deployment;
using ScadaLink.Commons.Entities.Instances;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class DeploymentRecordConfiguration : IEntityTypeConfiguration<DeploymentRecord>
{
public void Configure(EntityTypeBuilder<DeploymentRecord> builder)
{
builder.HasKey(d => d.Id);
builder.Property(d => d.DeploymentId)
.IsRequired()
.HasMaxLength(100);
builder.Property(d => d.RevisionHash)
.HasMaxLength(100);
builder.Property(d => d.DeployedBy)
.IsRequired()
.HasMaxLength(200);
builder.Property(d => d.Status)
.HasConversion<string>()
.HasMaxLength(50);
builder.HasOne<Instance>()
.WithMany()
.HasForeignKey(d => d.InstanceId)
.OnDelete(DeleteBehavior.Restrict);
// Optimistic concurrency on deployment status records
builder.Property<byte[]>("RowVersion")
.IsRowVersion();
builder.HasIndex(d => d.DeploymentId).IsUnique();
builder.HasIndex(d => d.InstanceId);
builder.HasIndex(d => d.DeployedAt);
}
}
public class SystemArtifactDeploymentRecordConfiguration : IEntityTypeConfiguration<SystemArtifactDeploymentRecord>
{
public void Configure(EntityTypeBuilder<SystemArtifactDeploymentRecord> builder)
{
builder.HasKey(d => d.Id);
builder.Property(d => d.ArtifactType)
.IsRequired()
.HasMaxLength(100);
builder.Property(d => d.DeployedBy)
.IsRequired()
.HasMaxLength(200);
builder.Property(d => d.PerSiteStatus)
.HasMaxLength(4000);
builder.HasIndex(d => d.DeployedAt);
}
}

View File

@@ -0,0 +1,81 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.ExternalSystems;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class ExternalSystemDefinitionConfiguration : IEntityTypeConfiguration<ExternalSystemDefinition>
{
public void Configure(EntityTypeBuilder<ExternalSystemDefinition> builder)
{
builder.HasKey(e => e.Id);
builder.Property(e => e.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(e => e.EndpointUrl)
.IsRequired()
.HasMaxLength(2000);
builder.Property(e => e.AuthType)
.IsRequired()
.HasMaxLength(50);
builder.Property(e => e.AuthConfiguration)
.HasMaxLength(4000);
builder.HasMany<ExternalSystemMethod>()
.WithOne()
.HasForeignKey(m => m.ExternalSystemDefinitionId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasIndex(e => e.Name).IsUnique();
}
}
public class ExternalSystemMethodConfiguration : IEntityTypeConfiguration<ExternalSystemMethod>
{
public void Configure(EntityTypeBuilder<ExternalSystemMethod> builder)
{
builder.HasKey(m => m.Id);
builder.Property(m => m.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(m => m.HttpMethod)
.IsRequired()
.HasMaxLength(10);
builder.Property(m => m.Path)
.IsRequired()
.HasMaxLength(2000);
builder.Property(m => m.ParameterDefinitions)
.HasMaxLength(4000);
builder.Property(m => m.ReturnDefinition)
.HasMaxLength(4000);
builder.HasIndex(m => new { m.ExternalSystemDefinitionId, m.Name }).IsUnique();
}
}
public class DatabaseConnectionDefinitionConfiguration : IEntityTypeConfiguration<DatabaseConnectionDefinition>
{
public void Configure(EntityTypeBuilder<DatabaseConnectionDefinition> builder)
{
builder.HasKey(d => d.Id);
builder.Property(d => d.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(d => d.ConnectionString)
.IsRequired()
.HasMaxLength(4000);
builder.HasIndex(d => d.Name).IsUnique();
}
}

View File

@@ -0,0 +1,50 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.InboundApi;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class ApiKeyConfiguration : IEntityTypeConfiguration<ApiKey>
{
public void Configure(EntityTypeBuilder<ApiKey> builder)
{
builder.HasKey(k => k.Id);
builder.Property(k => k.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(k => k.KeyValue)
.IsRequired()
.HasMaxLength(500);
builder.HasIndex(k => k.Name).IsUnique();
builder.HasIndex(k => k.KeyValue).IsUnique();
}
}
public class ApiMethodConfiguration : IEntityTypeConfiguration<ApiMethod>
{
public void Configure(EntityTypeBuilder<ApiMethod> builder)
{
builder.HasKey(m => m.Id);
builder.Property(m => m.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(m => m.Script)
.IsRequired();
builder.Property(m => m.ApprovedApiKeyIds)
.HasMaxLength(4000);
builder.Property(m => m.ParameterDefinitions)
.HasMaxLength(4000);
builder.Property(m => m.ReturnDefinition)
.HasMaxLength(4000);
builder.HasIndex(m => m.Name).IsUnique();
}
}

View File

@@ -0,0 +1,113 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.Instances;
using ScadaLink.Commons.Entities.Sites;
using ScadaLink.Commons.Entities.Templates;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class InstanceConfiguration : IEntityTypeConfiguration<Instance>
{
public void Configure(EntityTypeBuilder<Instance> builder)
{
builder.HasKey(i => i.Id);
builder.Property(i => i.UniqueName)
.IsRequired()
.HasMaxLength(200);
builder.Property(i => i.State)
.HasConversion<string>()
.HasMaxLength(50);
builder.HasOne<Template>()
.WithMany()
.HasForeignKey(i => i.TemplateId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<Site>()
.WithMany()
.HasForeignKey(i => i.SiteId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<Area>()
.WithMany()
.HasForeignKey(i => i.AreaId)
.OnDelete(DeleteBehavior.SetNull)
.IsRequired(false);
builder.HasMany(i => i.AttributeOverrides)
.WithOne()
.HasForeignKey(o => o.InstanceId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(i => i.ConnectionBindings)
.WithOne()
.HasForeignKey(b => b.InstanceId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasIndex(i => new { i.SiteId, i.UniqueName }).IsUnique();
}
}
public class InstanceAttributeOverrideConfiguration : IEntityTypeConfiguration<InstanceAttributeOverride>
{
public void Configure(EntityTypeBuilder<InstanceAttributeOverride> builder)
{
builder.HasKey(o => o.Id);
builder.Property(o => o.AttributeName)
.IsRequired()
.HasMaxLength(200);
builder.Property(o => o.OverrideValue)
.HasMaxLength(4000);
builder.HasIndex(o => new { o.InstanceId, o.AttributeName }).IsUnique();
}
}
public class InstanceConnectionBindingConfiguration : IEntityTypeConfiguration<InstanceConnectionBinding>
{
public void Configure(EntityTypeBuilder<InstanceConnectionBinding> builder)
{
builder.HasKey(b => b.Id);
builder.Property(b => b.AttributeName)
.IsRequired()
.HasMaxLength(200);
builder.HasOne<DataConnection>()
.WithMany()
.HasForeignKey(b => b.DataConnectionId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasIndex(b => new { b.InstanceId, b.AttributeName }).IsUnique();
}
}
public class AreaConfiguration : IEntityTypeConfiguration<Area>
{
public void Configure(EntityTypeBuilder<Area> builder)
{
builder.HasKey(a => a.Id);
builder.Property(a => a.Name)
.IsRequired()
.HasMaxLength(200);
builder.HasOne<Site>()
.WithMany()
.HasForeignKey(a => a.SiteId)
.OnDelete(DeleteBehavior.Cascade);
// Self-referencing parent area
builder.HasOne<Area>()
.WithMany(a => a.Children)
.HasForeignKey(a => a.ParentAreaId)
.OnDelete(DeleteBehavior.Restrict)
.IsRequired(false);
builder.HasIndex(a => new { a.SiteId, a.ParentAreaId, a.Name }).IsUnique();
}
}

View File

@@ -0,0 +1,66 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.Notifications;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class NotificationListConfiguration : IEntityTypeConfiguration<NotificationList>
{
public void Configure(EntityTypeBuilder<NotificationList> builder)
{
builder.HasKey(n => n.Id);
builder.Property(n => n.Name)
.IsRequired()
.HasMaxLength(200);
builder.HasMany(n => n.Recipients)
.WithOne()
.HasForeignKey(r => r.NotificationListId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasIndex(n => n.Name).IsUnique();
}
}
public class NotificationRecipientConfiguration : IEntityTypeConfiguration<NotificationRecipient>
{
public void Configure(EntityTypeBuilder<NotificationRecipient> builder)
{
builder.HasKey(r => r.Id);
builder.Property(r => r.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(r => r.EmailAddress)
.IsRequired()
.HasMaxLength(500);
}
}
public class SmtpConfigurationConfiguration : IEntityTypeConfiguration<SmtpConfiguration>
{
public void Configure(EntityTypeBuilder<SmtpConfiguration> builder)
{
builder.HasKey(s => s.Id);
builder.Property(s => s.Host)
.IsRequired()
.HasMaxLength(500);
builder.Property(s => s.AuthType)
.IsRequired()
.HasMaxLength(50);
builder.Property(s => s.Credentials)
.HasMaxLength(4000);
builder.Property(s => s.TlsMode)
.HasMaxLength(50);
builder.Property(s => s.FromAddress)
.IsRequired()
.HasMaxLength(500);
}
}

View File

@@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.Scripts;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class SharedScriptConfiguration : IEntityTypeConfiguration<SharedScript>
{
public void Configure(EntityTypeBuilder<SharedScript> builder)
{
builder.HasKey(s => s.Id);
builder.Property(s => s.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(s => s.Code)
.IsRequired();
builder.Property(s => s.ParameterDefinitions)
.HasMaxLength(4000);
builder.Property(s => s.ReturnDefinition)
.HasMaxLength(4000);
builder.HasIndex(s => s.Name).IsUnique();
}
}

View File

@@ -0,0 +1,44 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.Security;
using ScadaLink.Commons.Entities.Sites;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class LdapGroupMappingConfiguration : IEntityTypeConfiguration<LdapGroupMapping>
{
public void Configure(EntityTypeBuilder<LdapGroupMapping> builder)
{
builder.HasKey(m => m.Id);
builder.Property(m => m.LdapGroupName)
.IsRequired()
.HasMaxLength(500);
builder.Property(m => m.Role)
.IsRequired()
.HasMaxLength(100);
builder.HasIndex(m => m.LdapGroupName).IsUnique();
}
}
public class SiteScopeRuleConfiguration : IEntityTypeConfiguration<SiteScopeRule>
{
public void Configure(EntityTypeBuilder<SiteScopeRule> builder)
{
builder.HasKey(r => r.Id);
builder.HasOne<LdapGroupMapping>()
.WithMany()
.HasForeignKey(r => r.LdapGroupMappingId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<Site>()
.WithMany()
.HasForeignKey(r => r.SiteId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasIndex(r => new { r.LdapGroupMappingId, r.SiteId }).IsUnique();
}
}

View File

@@ -0,0 +1,68 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.Sites;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class SiteConfiguration : IEntityTypeConfiguration<Site>
{
public void Configure(EntityTypeBuilder<Site> builder)
{
builder.HasKey(s => s.Id);
builder.Property(s => s.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(s => s.SiteIdentifier)
.IsRequired()
.HasMaxLength(100);
builder.Property(s => s.Description)
.HasMaxLength(2000);
builder.HasIndex(s => s.Name).IsUnique();
builder.HasIndex(s => s.SiteIdentifier).IsUnique();
}
}
public class DataConnectionConfiguration : IEntityTypeConfiguration<DataConnection>
{
public void Configure(EntityTypeBuilder<DataConnection> builder)
{
builder.HasKey(d => d.Id);
builder.Property(d => d.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(d => d.Protocol)
.IsRequired()
.HasMaxLength(50);
builder.Property(d => d.Configuration)
.HasMaxLength(4000);
builder.HasIndex(d => d.Name).IsUnique();
}
}
public class SiteDataConnectionAssignmentConfiguration : IEntityTypeConfiguration<SiteDataConnectionAssignment>
{
public void Configure(EntityTypeBuilder<SiteDataConnectionAssignment> builder)
{
builder.HasKey(a => a.Id);
builder.HasOne<Site>()
.WithMany()
.HasForeignKey(a => a.SiteId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<DataConnection>()
.WithMany()
.HasForeignKey(a => a.DataConnectionId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasIndex(a => new { a.SiteId, a.DataConnectionId }).IsUnique();
}
}

View File

@@ -0,0 +1,149 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using ScadaLink.Commons.Entities.Templates;
namespace ScadaLink.ConfigurationDatabase.Configurations;
public class TemplateConfiguration : IEntityTypeConfiguration<Template>
{
public void Configure(EntityTypeBuilder<Template> builder)
{
builder.HasKey(t => t.Id);
builder.Property(t => t.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(t => t.Description)
.HasMaxLength(2000);
builder.HasIndex(t => t.Name).IsUnique();
// Self-referencing parent template (inheritance)
builder.HasOne<Template>()
.WithMany()
.HasForeignKey(t => t.ParentTemplateId)
.OnDelete(DeleteBehavior.Restrict)
.IsRequired(false);
builder.HasMany(t => t.Attributes)
.WithOne()
.HasForeignKey(a => a.TemplateId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(t => t.Alarms)
.WithOne()
.HasForeignKey(a => a.TemplateId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(t => t.Scripts)
.WithOne()
.HasForeignKey(s => s.TemplateId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasMany(t => t.Compositions)
.WithOne()
.HasForeignKey(c => c.TemplateId)
.OnDelete(DeleteBehavior.Cascade);
}
}
public class TemplateAttributeConfiguration : IEntityTypeConfiguration<TemplateAttribute>
{
public void Configure(EntityTypeBuilder<TemplateAttribute> builder)
{
builder.HasKey(a => a.Id);
builder.Property(a => a.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(a => a.Value)
.HasMaxLength(4000);
builder.Property(a => a.Description)
.HasMaxLength(2000);
builder.Property(a => a.DataSourceReference)
.HasMaxLength(500);
builder.Property(a => a.DataType)
.HasConversion<string>()
.HasMaxLength(50);
builder.HasIndex(a => new { a.TemplateId, a.Name }).IsUnique();
}
}
public class TemplateAlarmConfiguration : IEntityTypeConfiguration<TemplateAlarm>
{
public void Configure(EntityTypeBuilder<TemplateAlarm> builder)
{
builder.HasKey(a => a.Id);
builder.Property(a => a.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(a => a.Description)
.HasMaxLength(2000);
builder.Property(a => a.TriggerType)
.HasConversion<string>()
.HasMaxLength(50);
builder.Property(a => a.TriggerConfiguration)
.HasMaxLength(4000);
builder.HasIndex(a => new { a.TemplateId, a.Name }).IsUnique();
}
}
public class TemplateScriptConfiguration : IEntityTypeConfiguration<TemplateScript>
{
public void Configure(EntityTypeBuilder<TemplateScript> builder)
{
builder.HasKey(s => s.Id);
builder.Property(s => s.Name)
.IsRequired()
.HasMaxLength(200);
builder.Property(s => s.Code)
.IsRequired();
builder.Property(s => s.TriggerType)
.HasMaxLength(50);
builder.Property(s => s.TriggerConfiguration)
.HasMaxLength(4000);
builder.Property(s => s.ParameterDefinitions)
.HasMaxLength(4000);
builder.Property(s => s.ReturnDefinition)
.HasMaxLength(4000);
builder.HasIndex(s => new { s.TemplateId, s.Name }).IsUnique();
}
}
public class TemplateCompositionConfiguration : IEntityTypeConfiguration<TemplateComposition>
{
public void Configure(EntityTypeBuilder<TemplateComposition> builder)
{
builder.HasKey(c => c.Id);
builder.Property(c => c.InstanceName)
.IsRequired()
.HasMaxLength(200);
// The composed template reference
builder.HasOne<Template>()
.WithMany()
.HasForeignKey(c => c.ComposedTemplateId)
.OnDelete(DeleteBehavior.Restrict);
builder.HasIndex(c => new { c.TemplateId, c.InstanceName }).IsUnique();
}
}