fix #2483
All checks were successful
Build & Deploy on Dev / build (push) Successful in 47s

This commit is contained in:
Suphonchai Phoonsawat 2026-05-12 13:20:38 +07:00
parent 3b70246e9d
commit 5384a5893b
16 changed files with 98 additions and 41 deletions

View file

@ -752,6 +752,8 @@ namespace BMA.EHR.Recruit.Service.Controllers
var cols = workSheet.GetHeaderColumns();
int row = 2;
int batchCount = 0;
const int batchSize = 500;
while (row <= totalRows)
{
@ -860,13 +862,23 @@ namespace BMA.EHR.Recruit.Service.Controllers
//imported.Recruits.Add(r);
row++;
batchCount++;
// Batch save to prevent OutOfMemoryException on large imports
if (batchCount >= batchSize)
{
_context.SaveChanges();
_context.ChangeTracker.Clear();
// Re-attach the import entity after clearing the tracker
_context.RecruitImports.Attach(imported);
batchCount = 0;
}
}
}
}
// finally save to database
// Save remaining records in the last batch
_context.SaveChanges();
return Success();
@ -1371,6 +1383,8 @@ namespace BMA.EHR.Recruit.Service.Controllers
var cols = workSheet.GetHeaderColumns();
int row = 8;
int batchCount = 0;
const int batchSize = 500;
var endRow = workSheet.Dimension.End.Row; // แถวสุดท้ายที่มีข้อมูล
while (row <= endRow)
{
@ -1461,14 +1475,31 @@ namespace BMA.EHR.Recruit.Service.Controllers
}
row++;
batchCount++;
// Batch save to prevent OutOfMemoryException on large imports
if (batchCount >= batchSize)
{
rec_import.ScoreImport = imported;
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
// Re-attach entities after clearing the tracker
_context.Attach(rec_import);
_context.Attach(imported);
imported.Scores.Clear();
batchCount = 0;
}
} // end of sheet loop
} // end of all file loop
}
// finally save to database
rec_import.ScoreImport = imported;
await _context.SaveChangesAsync();
// Save remaining records in the last batch
if (imported.Scores.Count > 0)
{
rec_import.ScoreImport = imported;
await _context.SaveChangesAsync();
}
return Success();