fix(postgres): map DateTime to timestamp without time zone (Npgsql rejects Kind=Unspecified on timestamptz)

This commit is contained in:
2026-10-10 00:51:59 +03:30
parent 559a6e589c
commit 923b6b325b

View File

@@ -38,6 +38,21 @@ public sealed class GreenHomeDbContext : DbContext
{ {
base.OnModelCreating(modelBuilder); base.OnModelCreating(modelBuilder);
// Postgres: map DateTime -> "timestamp without time zone" (not timestamptz).
// Mirrors SQL Server's naive datetime2 — accepts any DateTime.Kind, so values
// copied from SQL Server (Kind=Unspecified) or written by the API won't crash.
if (Database.ProviderName == "Npgsql.EntityFrameworkCore.PostgreSQL")
{
foreach (var et in modelBuilder.Model.GetEntityTypes())
{
foreach (var p in et.GetProperties())
{
if (p.ClrType == typeof(DateTime) || p.ClrType == typeof(DateTime?))
p.SetColumnType("timestamp");
}
}
}
modelBuilder.Entity<Domain.Device>(b => modelBuilder.Entity<Domain.Device>(b =>
{ {
b.ToTable("Devices"); b.ToTable("Devices");