Skip to content

Commit 647e536

Browse files
Copilotmarkusrt
andcommitted
fix: make ReceivingDate non-nullable with migration initialization
- Change ReceivingDate from nullable to non-nullable DateTime - Update migration to add column as nullable first - Initialize existing entries with SamplingDate (or current date if null) - Alter column to be non-nullable after initialization Co-authored-by: markusrt <299753+markusrt@users.noreply.github.com>
1 parent 40cec57 commit 647e536

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

NRZMyk.Services/Data/Entities/SentinelEntry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class SentinelEntry : BaseEntity, IAggregateRoot, ISentinelEntry
1212
public DateTime? SamplingDate { get; set; }
1313

1414
[Display(Name = "Eingangsdatum")]
15-
public DateTime? ReceivingDate { get; set; }
15+
public DateTime ReceivingDate { get; set; }
1616

1717
[Display(Name = "Labnr. Einsender")]
1818
public string SenderLaboratoryNumber { get; set; }

NRZMyk.Services/Data/Migrations/20260207061615_SentinelEntry_AddReceivingDate.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,22 @@ public partial class SentinelEntry_AddReceivingDate : Migration
77
{
88
protected override void Up(MigrationBuilder migrationBuilder)
99
{
10+
// Add column as nullable first
1011
migrationBuilder.AddColumn<DateTime>(
1112
name: "ReceivingDate",
1213
table: "SentinelEntries",
1314
nullable: true);
15+
16+
// Initialize ReceivingDate with SamplingDate for existing entries
17+
migrationBuilder.Sql(
18+
@"UPDATE SentinelEntries
19+
SET ReceivingDate = COALESCE(SamplingDate, GETDATE())");
20+
21+
// Make the column non-nullable
22+
migrationBuilder.AlterColumn<DateTime>(
23+
name: "ReceivingDate",
24+
table: "SentinelEntries",
25+
nullable: false);
1426
}
1527

1628
protected override void Down(MigrationBuilder migrationBuilder)

0 commit comments

Comments
 (0)