Skip to content

Commit

Permalink
fix: add SetIdentityInsert method to handle manual ID insertion in Pa…
Browse files Browse the repository at this point in the history
…tientDb

- fix an issue where SeedPatients could create patients with unexpected IDs, causing seeded notes to fail linking correctly to patients. Notes rely on predefined IDs (1 to 4) to establish the relationship by :
  - adding new method SetIdentityInsert to enable manual insertion of ID values for Patients in the PatientDb database
  - adding transaction support and implementing identity_insert handling in DataSeeder for patient seeding
  • Loading branch information
EveCrystali committed Nov 14, 2024
1 parent 3474b54 commit b05d365
Showing 1 changed file with 41 additions and 18 deletions.
59 changes: 41 additions & 18 deletions BackendPatient/Data/DataSeeder.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using BackendPatient.Models;
using Microsoft.EntityFrameworkCore;
namespace BackendPatient.Data;

public class DataSeeder(ApplicationDbContext dbContext)
Expand Down Expand Up @@ -34,33 +35,55 @@ public void SeedPatients()
int batchSize = 10;

Check notice on line 35 in BackendPatient/Data/DataSeeder.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Convert local variable or field into constant (private accessibility)

Convert into constant

Check notice on line 35 in BackendPatient/Data/DataSeeder.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (for built-in types)

Use 'var' (built-in types)
int counter = 0;

Check notice on line 36 in BackendPatient/Data/DataSeeder.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (for built-in types)

Use 'var' (built-in types)

foreach (Patient patient in patientsFormatted)
using var transaction = _dbContext.Database.BeginTransaction();
try
{
// Avoid duplicate patients
if (existingPatients.Exists(p => p.FirstName == patient.FirstName &&
p.LastName == patient.LastName &&
p.DateOfBirth == patient.DateOfBirth))
SetIdentityInsert("Patients", true);

Check warning on line 41 in BackendPatient/Data/DataSeeder.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Redundant argument with default value

The parameter 'enable' has the same default value

foreach (Patient patient in patientsFormatted)

Check notice on line 43 in BackendPatient/Data/DataSeeder.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (when type is simple)

Use 'var' (simple types)
{
Console.WriteLine($"Skipping duplicate patient: {patient.FirstName} {patient.LastName} {patient.DateOfBirth}");
continue;
// Avoid duplicate patients
if (existingPatients.Exists(p => p.FirstName == patient.FirstName &&
p.LastName == patient.LastName &&
p.DateOfBirth == patient.DateOfBirth))
{
Console.WriteLine($"Skipping duplicate patient: {patient.FirstName} {patient.LastName} {patient.DateOfBirth}");
continue;
}
patient.Id = counter + 1;
_dbContext.Patients.Add(patient);
counter++;

// Save every 10 patients (batch size)
if (counter % batchSize == 0)

Check notice on line 58 in BackendPatient/Data/DataSeeder.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Invert 'if' statement to reduce nesting

Invert 'if' statement to reduce nesting
{
_dbContext.SaveChanges();
Console.WriteLine($"Saved {counter} patients so far...");
}
}
patient.Id = counter + 1;
_dbContext.Patients.Add(patient);
counter++;

// Save every 10 patients (batch size)
if (counter % batchSize == 0)
// Save any remaining patients
if (counter % batchSize != 0)
{
_dbContext.SaveChanges();
Console.WriteLine($"Saved {counter} patients so far...");
Console.WriteLine($"Final save, total patients saved: {counter}");
}
}

// Save any remaining patients
if (counter % batchSize != 0)
transaction.Commit();
}
catch
{
_dbContext.SaveChanges();
Console.WriteLine($"Final save, total patients saved: {counter}");
transaction.Rollback();
throw;
}

}

private void SetIdentityInsert(string tableName, bool enable = true)
{
string sql = enable

Check notice on line 84 in BackendPatient/Data/DataSeeder.cs

View workflow job for this annotation

GitHub Actions / Qodana for .NET

Use preferred 'var' style (for built-in types)

Use 'var' (built-in types)
? $"SET IDENTITY_INSERT {tableName} ON;"
: $"SET IDENTITY_INSERT {tableName} OFF;";
_dbContext.Database.ExecuteSqlRaw(sql);
}
}

0 comments on commit b05d365

Please sign in to comment.