From 144056c033b477126488e71b651aab53b5cc8688 Mon Sep 17 00:00:00 2001 From: Daniel Cazzulino Date: Sat, 21 Dec 2024 04:12:21 -0300 Subject: [PATCH] Add custom EF converter for Ulid showcase --- src/StructId.FunctionalTests/UlidTests.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/StructId.FunctionalTests/UlidTests.cs b/src/StructId.FunctionalTests/UlidTests.cs index 787189e..ba84c0c 100644 --- a/src/StructId.FunctionalTests/UlidTests.cs +++ b/src/StructId.FunctionalTests/UlidTests.cs @@ -43,13 +43,18 @@ public override void SetValue(IDbDataParameter parameter, Ulid value) // showcases a custom EF value converter trumps the built-in templatized // support for types that provide IParsable and IFormattable -//public partial class UlidToStringConverter : ValueConverter -//{ -// public UlidToStringConverter() : this(null) { } +public partial class UlidToStringConverter : ValueConverter +{ + public UlidToStringConverter() : this(null) { } -// public UlidToStringConverter(ConverterMappingHints? mappingHints = null) -// : base(id => id.ToString(), value => Ulid.Parse(value), mappingHints) { } -//} + public UlidToStringConverter(ConverterMappingHints? mappingHints = null) + : base(id => id.ToString(), value => Ulid.Parse(value), mappingHints) + { + Instantiated = true; + } + + public static bool Instantiated { get; private set; } +} // showcases alternative serialization //public class BinaryUlidHandler : TypeHandler @@ -147,6 +152,8 @@ public void EntityFramework() var product3 = context.Products.FirstOrDefault(x => guid == x.Id); Assert.Equal(product, product3); + + Assert.True(UlidToStringConverter.Instantiated, "Custom EF converter for Ulid was not used."); } public class UlidContext : DbContext