Compare commits

..

No commits in common. "dev" and "v1.0.20" have entirely different histories.
dev ... v1.0.20

7 changed files with 10 additions and 1700 deletions

View file

@ -20,7 +20,6 @@ namespace BMA.EHR.Recruit.Data
modelBuilder.Entity<Models.Recruits.Recruit>().HasMany(x => x.Addresses).WithOne(x => x.Recruit).OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Models.Recruits.Recruit>().HasMany(x => x.Certificates).WithOne(x => x.Recruit).OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<Models.Recruits.Recruit>().HasMany(x => x.Payments).WithOne(x => x.Recruit).OnDelete(DeleteBehavior.Cascade);
modelBuilder.Entity<ScoreImport>().HasMany(x => x.Scores).WithOne(x => x.ScoreImport).HasForeignKey(x => x.ScoreImportId).OnDelete(DeleteBehavior.Cascade);
}
public DbSet<Document> Documents { get; set; }

File diff suppressed because it is too large Load diff

View file

@ -1,64 +0,0 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace BMA.EHR.Recruit.Migrations
{
/// <inheritdoc />
public partial class fixrelation : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ScoreImports_Documents_ImportFileId",
table: "ScoreImports");
migrationBuilder.AlterColumn<Guid>(
name: "ImportFileId",
table: "ScoreImports",
type: "char(36)",
nullable: true,
collation: "ascii_general_ci",
oldClrType: typeof(Guid),
oldType: "char(36)")
.OldAnnotation("Relational:Collation", "ascii_general_ci");
migrationBuilder.AddForeignKey(
name: "FK_ScoreImports_Documents_ImportFileId",
table: "ScoreImports",
column: "ImportFileId",
principalTable: "Documents",
principalColumn: "Id");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_ScoreImports_Documents_ImportFileId",
table: "ScoreImports");
migrationBuilder.AlterColumn<Guid>(
name: "ImportFileId",
table: "ScoreImports",
type: "char(36)",
nullable: false,
defaultValue: new Guid("00000000-0000-0000-0000-000000000000"),
collation: "ascii_general_ci",
oldClrType: typeof(Guid),
oldType: "char(36)",
oldNullable: true)
.OldAnnotation("Relational:Collation", "ascii_general_ci");
migrationBuilder.AddForeignKey(
name: "FK_ScoreImports_Documents_ImportFileId",
table: "ScoreImports",
column: "ImportFileId",
principalTable: "Documents",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
}
}
}

View file

@ -1353,7 +1353,7 @@ namespace BMA.EHR.Recruit.Migrations
.HasColumnOrder(101)
.HasComment("User Id ที่สร้างข้อมูล");
b.Property<Guid?>("ImportFileId")
b.Property<Guid>("ImportFileId")
.HasColumnType("char(36)");
b.Property<string>("LastUpdateFullName")
@ -1550,7 +1550,9 @@ namespace BMA.EHR.Recruit.Migrations
{
b.HasOne("BMA.EHR.Recruit.Models.Documents.Document", "ImportFile")
.WithMany()
.HasForeignKey("ImportFileId");
.HasForeignKey("ImportFileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.HasOne("BMA.EHR.Recruit.Models.Recruits.RecruitImport", "RecruitImport")
.WithOne("ScoreImport")

View file

@ -1,6 +1,5 @@
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BMA.EHR.Recruit.Models.Recruits
{
@ -84,9 +83,6 @@ namespace BMA.EHR.Recruit.Models.Recruits
[MaxLength(50), Comment("สถานะคัดกรองคุณสมบัติ")]
public string ExamAttribute { get; set; } = string.Empty;
[ForeignKey("ScoreImport")]
public Guid ScoreImportId { get; set; }
public ScoreImport ScoreImport { get; set; }
}
}

View file

@ -7,9 +7,7 @@ namespace BMA.EHR.Recruit.Models.Recruits
{
public int Year { get; set; }
public Guid? ImportFileId { get; set; }
public Document ImportFile { get; set; }
public Document ImportFile { get; set; } = new Document();
public virtual List<RecruitScore> Scores { get; set; } = new List<RecruitScore>();

View file

@ -580,13 +580,8 @@ public class ImportBackgroundService : BackgroundService
if (existingScores.Count > 0)
await _context.BulkDeleteAsync(existingScores);
// Also delete the ScoreImport row itself (has unique index on RecruitImportId)
// Use Remove+SaveChanges instead of BulkDelete since BulkDelete fails with detached entities
var scoreImportToDelete = existingScoreImport;
_context.ChangeTracker.Clear();
var deleteStub = new ScoreImport { Id = scoreImportToDelete.Id };
_context.ScoreImports.Attach(deleteStub);
_context.ScoreImports.Remove(deleteStub);
await _context.SaveChangesAsync();
await _context.BulkDeleteAsync(new List<ScoreImport> { existingScoreImport });
}
// Clear tracker to avoid stale references after BulkDelete (which bypasses EF tracking)
@ -607,9 +602,7 @@ 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;
@ -620,9 +613,9 @@ public class ImportBackgroundService : BackgroundService
imported.LastUpdateFullName = job.FullName ?? "System Administrator";
imported.Scores = new List<RecruitScore>();
// Save ScoreImport — set ImportFileId FK directly (explicit property, not shadow)
imported.ImportFileId = doc.Id;
_context.ScoreImports.Add(imported);
// Save ScoreImport — use shadow FK for ImportFile to avoid re-inserting Document
var scoreEntry = _context.ScoreImports.Add(imported);
scoreEntry.Property("ImportFileId").CurrentValue = doc.Id;
await _context.SaveChangesAsync();
_context.ChangeTracker.Clear();
@ -664,7 +657,6 @@ public class ImportBackgroundService : BackgroundService
if (string.IsNullOrEmpty(cell1)) break;
var r = new RecruitScore();
r.Id = Guid.NewGuid();
r.ExamId = reader.GetValue(1)?.ToString();
if (!string.IsNullOrEmpty(r.ExamId) && recruitsDict.TryGetValue(r.ExamId, out var recruit))
@ -707,6 +699,7 @@ public class ImportBackgroundService : BackgroundService
r.LastUpdatedAt = DateTime.Now;
r.LastUpdateUserId = job.UserId ?? "";
r.LastUpdateFullName = job.FullName ?? "System Administrator";
r.ScoreImport = imported;
batchScores.Add(r);
}
@ -716,11 +709,6 @@ public class ImportBackgroundService : BackgroundService
if (batchCount >= batchSize)
{
// Set ScoreImportId FK for all scores in batch
foreach (var score in batchScores)
{
score.ScoreImportId = scoreImport_id;
}
await _context.BulkInsertAsync(batchScores);
batchScores.Clear();
batchCount = 0;
@ -731,10 +719,6 @@ public class ImportBackgroundService : BackgroundService
// Process remaining records
if (batchScores.Count > 0)
{
foreach (var score in batchScores)
{
score.ScoreImportId = scoreImport_id;
}
await _context.BulkInsertAsync(batchScores);
}
} while (reader.NextResult());