diff --git a/src/GreenHome.Infrastructure/Data/GreenHomeDbContext.cs b/src/GreenHome.Infrastructure/Data/GreenHomeDbContext.cs index 6be7ed8..e4ffe48 100644 --- a/src/GreenHome.Infrastructure/Data/GreenHomeDbContext.cs +++ b/src/GreenHome.Infrastructure/Data/GreenHomeDbContext.cs @@ -38,9 +38,10 @@ public sealed class GreenHomeDbContext : DbContext { 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. + // Postgres-specific mappings (SQL Server path is untouched): + // 1) DateTime -> "timestamp" (no tz): mirrors naive datetime2, accepts any Kind. + // 2) varchar: Postgres counts BYTES, SQL Server's nvarchar counts CHARACTERS. + // x4 keeps char-count semantics for any UTF-8 charset (Persian = 2 bytes/char). if (Database.ProviderName == "Npgsql.EntityFrameworkCore.PostgreSQL") { foreach (var et in modelBuilder.Model.GetEntityTypes()) @@ -49,6 +50,8 @@ public sealed class GreenHomeDbContext : DbContext { if (p.ClrType == typeof(DateTime) || p.ClrType == typeof(DateTime?)) p.SetColumnType("timestamp"); + else if (p.ClrType == typeof(string) && p.GetMaxLength() is int len) + p.SetColumnType($"character varying({len * 4})"); } } }