From 2061ee93848ab636cccd8275378332969fb1236f Mon Sep 17 00:00:00 2001 From: Todd Aspeotis Date: Wed, 6 Dec 2023 08:58:46 +1000 Subject: [PATCH] Handle Nullable --- .../NpgsqlJsonElementHackConvention.cs | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/EFCore.PG/Metadata/Conventions/NpgsqlJsonElementHackConvention.cs b/src/EFCore.PG/Metadata/Conventions/NpgsqlJsonElementHackConvention.cs index df2fb162a..48c3b207e 100644 --- a/src/EFCore.PG/Metadata/Conventions/NpgsqlJsonElementHackConvention.cs +++ b/src/EFCore.PG/Metadata/Conventions/NpgsqlJsonElementHackConvention.cs @@ -14,16 +14,27 @@ namespace Npgsql.EntityFrameworkCore.PostgreSQL.Metadata.Conventions; /// public class NpgsqlJsonElementHackConvention : IPropertyAddedConvention { - private NpgsqlJsonTypeMapping? _jsonTypeMapping; + private NpgsqlJsonTypeMapping? _jsonElementJsonTypeMapping; + private NpgsqlJsonTypeMapping? _nullableJsonElementJsonTypeMapping; /// public void ProcessPropertyAdded(IConventionPropertyBuilder propertyBuilder, IConventionContext context) { var property = propertyBuilder.Metadata; + var clrType = property.ClrType; - if (property.ClrType == typeof(JsonElement) && property.GetColumnType() is null) + if (property.GetColumnType() is not null) { - property.SetTypeMapping(_jsonTypeMapping ??= new NpgsqlJsonTypeMapping("jsonb", typeof(JsonElement))); + return; + } + + if (clrType == typeof(JsonElement)) + { + property.SetTypeMapping(_jsonElementJsonTypeMapping ??= new NpgsqlJsonTypeMapping("jsonb", clrType)); + } + else if (clrType == typeof(JsonElement?)) + { + property.SetTypeMapping(_nullableJsonElementJsonTypeMapping ??= new NpgsqlJsonTypeMapping("jsonb", clrType)); } } }