fix relation
All checks were successful
Build & Deploy on Dev / build (push) Successful in 41s

This commit is contained in:
Suphonchai Phoonsawat 2026-05-19 17:23:36 +07:00
parent 054ef81c63
commit d019ed588b
6 changed files with 1680 additions and 5 deletions

View file

@ -20,6 +20,7 @@ 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

@ -0,0 +1,64 @@
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,9 +1550,7 @@ namespace BMA.EHR.Recruit.Migrations
{
b.HasOne("BMA.EHR.Recruit.Models.Documents.Document", "ImportFile")
.WithMany()
.HasForeignKey("ImportFileId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
.HasForeignKey("ImportFileId");
b.HasOne("BMA.EHR.Recruit.Models.Recruits.RecruitImport", "RecruitImport")
.WithOne("ScoreImport")

View file

@ -1,5 +1,6 @@
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace BMA.EHR.Recruit.Models.Recruits
{
@ -83,6 +84,7 @@ 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

@ -580,8 +580,13 @@ 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();
await _context.BulkDeleteAsync(new List<ScoreImport> { existingScoreImport });
var deleteStub = new ScoreImport { Id = scoreImportToDelete.Id };
_context.ScoreImports.Attach(deleteStub);
_context.ScoreImports.Remove(deleteStub);
await _context.SaveChangesAsync();
}
// Clear tracker to avoid stale references after BulkDelete (which bypasses EF tracking)