fix Update FK ติด Error
All checks were successful
Build & Deploy on Dev / build (push) Successful in 44s

This commit is contained in:
harid 2026-05-19 13:40:50 +07:00
parent 4ea7c0010b
commit eea7fbcfa1

View file

@ -790,15 +790,28 @@ public class ImportBackgroundService : BackgroundService
await _context.SaveChangesAsync(); await _context.SaveChangesAsync();
_context.ChangeTracker.Clear(); _context.ChangeTracker.Clear();
// preload scores - re-query from DB to avoid tracking issues // หา ScoreImportId ก่อน
var scoreImportId = await _context.ScoreImports
.Where(x => x.RecruitImportId == rec_import.Id)
.Select(x => x.Id)
.FirstOrDefaultAsync();
if (scoreImportId == Guid.Empty) return;
// preload scores - query all entities that will be updated (with tracking)
var scoreList = await _context.RecruitScores var scoreList = await _context.RecruitScores
.Where(s => s.ScoreImport.RecruitImportId == rec_import.Id && !string.IsNullOrEmpty(s.ExamId)) .Where(s => EF.Property<Guid>(s, "ScoreImportId") == scoreImportId && !string.IsNullOrEmpty(s.ExamId))
.GroupBy(x => x.ExamId) .GroupBy(x => x.ExamId)
.Where(g => g.Count() == 1) .Where(g => g.Count() == 1)
.Select(g => g.First()) .Select(g => g.First())
.ToListAsync(); .ToListAsync();
// Group by ExamId for easy lookup
var score = scoreList.ToDictionary(s => s.ExamId!, s => s); var score = scoreList.ToDictionary(s => s.ExamId!, s => s);
// ถ้าไม่มีผลคะแนนสอบคัดเลือกผู้พิการให้จบการทำงาน
if (score.Count == 0) return;
// Read from saved file (ResultFile uses stream from Form, but we saved to disk) // Read from saved file (ResultFile uses stream from Form, but we saved to disk)
using var stream = System.IO.File.OpenRead(job.ImportFile); using var stream = System.IO.File.OpenRead(job.ImportFile);
using var c_package = new ExcelPackage(stream); using var c_package = new ExcelPackage(stream);
@ -809,7 +822,6 @@ public class ImportBackgroundService : BackgroundService
int batchCount = 0; int batchCount = 0;
const int batchSize = 500; const int batchSize = 500;
var endRow = workSheet.Dimension.End.Row; var endRow = workSheet.Dimension.End.Row;
var batchUpdates = new List<RecruitScore>();
while (row <= endRow) while (row <= endRow)
{ {
@ -827,7 +839,6 @@ public class ImportBackgroundService : BackgroundService
existingScore.LastUpdatedAt = DateTime.Now; existingScore.LastUpdatedAt = DateTime.Now;
existingScore.LastUpdateUserId = job.UserId ?? ""; existingScore.LastUpdateUserId = job.UserId ?? "";
existingScore.LastUpdateFullName = job.FullName ?? "System Administrator"; existingScore.LastUpdateFullName = job.FullName ?? "System Administrator";
batchUpdates.Add(existingScore);
batchCount++; batchCount++;
} }
@ -835,16 +846,17 @@ public class ImportBackgroundService : BackgroundService
if (batchCount >= batchSize) if (batchCount >= batchSize)
{ {
await _context.BulkUpdateAsync(batchUpdates); await _context.SaveChangesAsync();
batchUpdates.Clear(); _context.ChangeTracker.Clear();
batchCount = 0; batchCount = 0;
} }
} }
// Process remaining records // Process remaining records
if (batchUpdates.Count > 0) if (batchCount > 0)
{ {
await _context.BulkUpdateAsync(batchUpdates); await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
} }
} }
} }