From 461790c1c1a7a10a0e0a41566e2a657308784c8e Mon Sep 17 00:00:00 2001 From: Suphonchai Phoonsawat Date: Wed, 13 May 2026 06:39:54 +0700 Subject: [PATCH] fix logic --- Services/ImportBackgroundService.cs | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 3c2c0c2..8023d57 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -247,16 +247,6 @@ public class ImportBackgroundService : BackgroundService var imported = await _context.RecruitImports.AsQueryable() .Include(x => x.ImportHostories) .Include(x => x.ImportFile) - .Include(x => x.Recruits) - .ThenInclude(x => x.Addresses) - .Include(x => x.Recruits) - .ThenInclude(x => x.Occupations) - .Include(x => x.Recruits) - .ThenInclude(x => x.Certificates) - .Include(x => x.Recruits) - .ThenInclude(x => x.Educations) - .Include(x => x.Recruits) - .ThenInclude(x => x.Payments) .FirstOrDefaultAsync(x => x.Id == job.RecruitImportId); if (imported == null) throw new Exception("RecruitImport not found"); @@ -281,8 +271,9 @@ public class ImportBackgroundService : BackgroundService int row = 2; int batchCount = 0; - const int batchSize = 100; + const int batchSize = 50; int totalProcessed = 0; + var batchList = new List(); while (row <= totalRows) { @@ -410,7 +401,7 @@ public class ImportBackgroundService : BackgroundService }); r.RecruitImport = imported; - _context.Recruits.Add(r); + batchList.Add(r); row++; batchCount++; @@ -418,13 +409,20 @@ public class ImportBackgroundService : BackgroundService if (batchCount >= batchSize) { + _context.Recruits.AddRange(batchList); _context.SaveChanges(); _context.ChangeTracker.Clear(); _context.Entry(imported).State = EntityState.Unchanged; + batchList.Clear(); batchCount = 0; _tracker.UpdateStatus(job.JobId, ImportJobStatus.Running, totalProcessed); } } + + if (batchList.Count > 0) + { + _context.Recruits.AddRange(batchList); + } } _context.SaveChanges();