using Microsoft.EntityFrameworkCore.ChangeTracking; using Microsoft.EntityFrameworkCore.Metadata; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json.Serialization; using System.Text.Json; using System.Threading.Tasks; namespace Persistence.Database; public static class EFExtensions { private static readonly JsonSerializerOptions jsonSerializerOptions = new() { AllowTrailingCommas = true, WriteIndented = true, NumberHandling = JsonNumberHandling.AllowReadingFromString | JsonNumberHandling.AllowNamedFloatingPointLiterals, }; public static Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder HasJsonConversion( this Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder builder) => HasJsonConversion(builder, jsonSerializerOptions); public static Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder HasJsonConversion( this Microsoft.EntityFrameworkCore.Metadata.Builders.PropertyBuilder builder, JsonSerializerOptions jsonSerializerOptions) { builder.HasConversion( s => JsonSerializer.Serialize(s, jsonSerializerOptions), s => JsonSerializer.Deserialize(s, jsonSerializerOptions)!); ValueComparer valueComparer = new( (a, b) => (a != null) && (b != null) ? a.GetHashCode() == b.GetHashCode() : (a == null) && (b == null), i => (i == null) ? -1 : i.GetHashCode(), i => i); builder.Metadata.SetValueComparer(valueComparer); return builder; } }