fix(postgres): map DateTime to timestamp without time zone (Npgsql rejects Kind=Unspecified on timestamptz)
This commit is contained in:
@@ -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");
|
||||||
|
|||||||
Reference in New Issue
Block a user