diff --git a/Services/ImportBackgroundService.cs b/Services/ImportBackgroundService.cs index 4d4d4bc..6841bc5 100644 --- a/Services/ImportBackgroundService.cs +++ b/Services/ImportBackgroundService.cs @@ -602,7 +602,9 @@ public class ImportBackgroundService : BackgroundService // get doc from minio (MinIOService already saves Document to its own context) var doc = await _minioService.UploadFileAsync(new DummyFormFile(job.ImportFile)); + var scoreImport_id = Guid.NewGuid(); // Pre-generate Id to use as FK var imported = new ScoreImport(); + imported.Id = scoreImport_id; imported.Year = rec_import_year; imported.RecruitImportId = rec_import_id; imported.CreatedAt = DateTime.Now; @@ -699,7 +701,6 @@ public class ImportBackgroundService : BackgroundService r.LastUpdatedAt = DateTime.Now; r.LastUpdateUserId = job.UserId ?? ""; r.LastUpdateFullName = job.FullName ?? "System Administrator"; - r.ScoreImport = imported; batchScores.Add(r); } @@ -709,6 +710,11 @@ public class ImportBackgroundService : BackgroundService if (batchCount >= batchSize) { + // Set ScoreImportId FK for all scores in batch + foreach (var score in batchScores) + { + _context.Entry(score).Property("ScoreImportId").CurrentValue = scoreImport_id; + } await _context.BulkInsertAsync(batchScores); batchScores.Clear(); batchCount = 0; @@ -719,6 +725,10 @@ public class ImportBackgroundService : BackgroundService // Process remaining records if (batchScores.Count > 0) { + foreach (var score in batchScores) + { + _context.Entry(score).Property("ScoreImportId").CurrentValue = scoreImport_id; + } await _context.BulkInsertAsync(batchScores); } } while (reader.NextResult());