fix(db): update migration and configure many-to-many relationships to avoid cascade paths

- Regenerated InitialCreate migration to reflect new entity configurations
- Configured many-to-many relationship between CostCycle and Unit with NoAction on Unit side to prevent multiple cascade paths
- Updated UnitCost relationships: Cascade from CostCycle, NoAction from Unit
- Removed commented-out global filter code and replaced with explicit relationship configurations
This commit is contained in:
2026-06-09 02:23:09 +03:30
parent 569e39b58d
commit 6644db7e78
5 changed files with 61 additions and 52 deletions

View File

@@ -53,19 +53,30 @@ namespace Complex.Infrastructure
builder.Entity<Unit>().HasQueryFilter(b => EF.Property<bool>(b, "IsRemoved") == false);
builder.Entity<UnitCost>().HasQueryFilter(b => EF.Property<bool>(b, "IsRemoved") == false);
builder.Entity<CostCycle>().HasQueryFilter(b => EF.Property<bool>(b, "IsRemoved") == false);
//builder.ApplyGlobalFilters<bool>("IsRemoved", false);
// var allEntities = modelBuilder.Model.GetEntityTypes();
//Expression<Func<IMutableEntityType, bool>> filterExpr = bm => (bm.GetProperty("IsRemoved").ToString() == "0");
//foreach (var entity in builder.Model.GetEntityTypes())
//{
// //if (!entity.ClrType.IsAssignableTo(typeof(BaseEntity)))
// // continue;
// if (entity.ClrType.GetCustomAttributes(typeof(AuditableAttribute), true).Length > 0)
// entity
// //entity.SetQueryFilter(filterExpr);
//}
// Configure many-to-many between CostCycle and Unit
// SQL Server does not allow multiple cascade paths, so one side must use NoAction
builder.Entity<CostCycle>()
.HasMany(cc => cc.Units)
.WithMany(u => u.CostCycles)
.UsingEntity<Dictionary<string, object>>(
"CostCycleUnit",
j => j.HasOne<Unit>().WithMany().HasForeignKey("UnitsId").OnDelete(DeleteBehavior.NoAction),
j => j.HasOne<CostCycle>().WithMany().HasForeignKey("CostCyclesId").OnDelete(DeleteBehavior.Cascade));
// Configure UnitCost relationships to avoid multiple cascade paths
// UnitCost -> CostCycle (Cascade) and UnitCost -> Unit (NoAction)
builder.Entity<UnitCost>()
.HasOne(uc => uc.CostCycle)
.WithMany()
.HasForeignKey(uc => uc.CostCycleId)
.OnDelete(DeleteBehavior.Cascade);
builder.Entity<UnitCost>()
.HasOne(uc => uc.Unit)
.WithMany()
.HasForeignKey(uc => uc.UnitId)
.OnDelete(DeleteBehavior.NoAction);
}