From 7ed86f18d7dcd80e4a3d39ee1351b04cb290dbd6 Mon Sep 17 00:00:00 2001 From: TekH Date: Fri, 1 Aug 2025 10:54:08 +0200 Subject: [PATCH] Refactor ProfileControlsTF and ProfileObjState relationships Updated property names and foreign key references in ProfileControlsTF and ProfileObjState classes to improve database relationship integrity. Added TFControls collection to ProfileObjState and modified queries in ProfileObjRepository to include related entities. Established relationships in WFDBContext with cascade delete behavior. --- src/WorkFlow.Domain/Entities/ProfileControlsTF.cs | 4 ++-- src/WorkFlow.Domain/Entities/ProfileObjState.cs | 4 +++- .../Repositories/ProfileObjRepository.cs | 1 + src/WorkFlow.Infrastructure/WFDBContext.cs | 8 ++++++++ 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/WorkFlow.Domain/Entities/ProfileControlsTF.cs b/src/WorkFlow.Domain/Entities/ProfileControlsTF.cs index 60e95be..a348f9e 100644 --- a/src/WorkFlow.Domain/Entities/ProfileControlsTF.cs +++ b/src/WorkFlow.Domain/Entities/ProfileControlsTF.cs @@ -17,7 +17,7 @@ public class ProfileControlsTF /// Validation should be enforced at the application level to ensure a value is provided. /// [Column("OBJ_STATE_ID", TypeName = "bigint")] - public long? StateId { get; set; } + public long? ObjStateId { get; set; } /// /// Although this field is marked as nullable in the database schema to allow @@ -97,6 +97,6 @@ public class ProfileControlsTF [Column("ADDED_WHEN", TypeName = "datetime")] public DateTime AddedWhen { get; set; } - [ForeignKey("StateId")] + [ForeignKey("ObjStateId")] public virtual ProfileObjState? State { get; set; } } \ No newline at end of file diff --git a/src/WorkFlow.Domain/Entities/ProfileObjState.cs b/src/WorkFlow.Domain/Entities/ProfileObjState.cs index bf67488..5eec74c 100644 --- a/src/WorkFlow.Domain/Entities/ProfileObjState.cs +++ b/src/WorkFlow.Domain/Entities/ProfileObjState.cs @@ -79,9 +79,11 @@ public class ProfileObjState [Column("CHANGED_WHEN", TypeName = "datetime")] public DateTime? ChangedWhen { get; set; } - [ForeignKey("StateId")] + [ForeignKey("ObjStateId")] public virtual State? State1 { get; set; } + public IEnumerable TFControls { get; set; } = Array.Empty(); + public IEnumerable ToList() => new List { State1?.IntlState, State2, State3, State4 diff --git a/src/WorkFlow.Infrastructure/Repositories/ProfileObjRepository.cs b/src/WorkFlow.Infrastructure/Repositories/ProfileObjRepository.cs index 50d6a6b..8bd034d 100644 --- a/src/WorkFlow.Infrastructure/Repositories/ProfileObjRepository.cs +++ b/src/WorkFlow.Infrastructure/Repositories/ProfileObjRepository.cs @@ -36,5 +36,6 @@ public class ProfileObjRepository : IProfileObjRepository => await _context.Objects .FromSqlRaw("SELECT * FROM [FNMWF_GET_PROFILE_OBJECTS] ({0}, {1})", userId, profileId) .Include(obj => obj.States).ThenInclude(objState => objState != null ? objState.State1 : null) + .Include(obj => obj.States).ThenInclude(objState => objState != null ? objState.TFControls : null) .ToListAsync(cancel); } \ No newline at end of file diff --git a/src/WorkFlow.Infrastructure/WFDBContext.cs b/src/WorkFlow.Infrastructure/WFDBContext.cs index dd77243..19bf3e1 100644 --- a/src/WorkFlow.Infrastructure/WFDBContext.cs +++ b/src/WorkFlow.Infrastructure/WFDBContext.cs @@ -2,6 +2,7 @@ using DigitalData.UserManager.Infrastructure; using DigitalData.UserManager.Infrastructure.Contracts; using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal; using WorkFlow.Domain.Entities; namespace WorkFlow.Infrastructure; @@ -45,6 +46,13 @@ public class WFDBContext : DbContext, IUserManagerDbContext //configure model builder for user manager tables modelBuilder.ConfigureUserManager(); + modelBuilder.Entity() + .HasMany(pos => pos.ControlsTF) + .WithOne() + .HasForeignKey(pctf => pctf.ObjStateId) + .HasPrincipalKey(pos => pos.Id) + .OnDelete(DeleteBehavior.Cascade); + base.OnModelCreating(modelBuilder); } } \ No newline at end of file