fix Update FK ติด Error
All checks were successful
Build & Deploy on Dev / build (push) Successful in 44s
All checks were successful
Build & Deploy on Dev / build (push) Successful in 44s
This commit is contained in:
parent
4ea7c0010b
commit
eea7fbcfa1
1 changed files with 20 additions and 8 deletions
|
|
@ -790,15 +790,28 @@ public class ImportBackgroundService : BackgroundService
|
|||
await _context.SaveChangesAsync();
|
||||
_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
|
||||
.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)
|
||||
.Where(g => g.Count() == 1)
|
||||
.Select(g => g.First())
|
||||
.ToListAsync();
|
||||
|
||||
// Group by ExamId for easy lookup
|
||||
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)
|
||||
using var stream = System.IO.File.OpenRead(job.ImportFile);
|
||||
using var c_package = new ExcelPackage(stream);
|
||||
|
|
@ -809,7 +822,6 @@ public class ImportBackgroundService : BackgroundService
|
|||
int batchCount = 0;
|
||||
const int batchSize = 500;
|
||||
var endRow = workSheet.Dimension.End.Row;
|
||||
var batchUpdates = new List<RecruitScore>();
|
||||
|
||||
while (row <= endRow)
|
||||
{
|
||||
|
|
@ -827,7 +839,6 @@ public class ImportBackgroundService : BackgroundService
|
|||
existingScore.LastUpdatedAt = DateTime.Now;
|
||||
existingScore.LastUpdateUserId = job.UserId ?? "";
|
||||
existingScore.LastUpdateFullName = job.FullName ?? "System Administrator";
|
||||
batchUpdates.Add(existingScore);
|
||||
batchCount++;
|
||||
}
|
||||
|
||||
|
|
@ -835,16 +846,17 @@ public class ImportBackgroundService : BackgroundService
|
|||
|
||||
if (batchCount >= batchSize)
|
||||
{
|
||||
await _context.BulkUpdateAsync(batchUpdates);
|
||||
batchUpdates.Clear();
|
||||
await _context.SaveChangesAsync();
|
||||
_context.ChangeTracker.Clear();
|
||||
batchCount = 0;
|
||||
}
|
||||
}
|
||||
|
||||
// Process remaining records
|
||||
if (batchUpdates.Count > 0)
|
||||
if (batchCount > 0)
|
||||
{
|
||||
await _context.BulkUpdateAsync(batchUpdates);
|
||||
await _context.SaveChangesAsync();
|
||||
_context.ChangeTracker.Clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue