fix(postgres): varchar byte-vs-char mismatch — scale string lengths x4 for Postgres
This commit is contained in:
@@ -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})");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user