I am using an EF Core value conversion.
https://learn.microsoft.com/en-us/ef/core/modeling/value-conversions
I wrote a JSON serializer converter as follows:
private static ValueConverter<T, String> JsonValueConverter<T>()
{
ValueConverter<T, String> jsonConverter = new ValueConverter<T, String>(
v => JsonConvert.SerializeObject(v),
v => JsonConvert.DeserializeObject<T>(v));
return jsonConverter;
}
Implemented in the app with:
protected override void OnModelCreating(ModelBuilder mb)
{
...
mb.Entity<MyObject>()
.Property(p => p.MySerializableObject)
.HasConversion(JsonValueConverter<MySerializableObject>());
...
}
It works.
However, when you make changes to a property inside the serialized object, EF Core change tracking does not pick up changes to MySerializableObject
.
I assume there is some way to force this on the object level. I have attempted to implement IEqualityComparer
on MySerializableObject
but change tracking didn't start working.
I was able to get change tracking to kick in by adding a ValueConverter
and a ValueComparer
to the Metadata of the property, however, my comparer is probably very inefficient. If anyone has suggestions on performance tuning for this, it would be much appreciated.
mb.Entity<MyObject>().Property(p => .MySerializableObject).HasJsonConversion();
With the new HasJsonConversion extension method
public static class ValueConversionExtensions
{
public static PropertyBuilder<T> HasJsonConversion<T>(this PropertyBuilder<T> propertyBuilder)
{
ValueConverter<T, String> converter = new ValueConverter<T, String>(
v => JsonConvert.SerializeObject(v),
v => JsonConvert.DeserializeObject<T>(v));
ValueComparer<T> comparer = new ValueComparer<T>(
(l, r) => JsonConvert.SerializeObject(l) == JsonConvert.SerializeObject(r),
v => v == null ? 0 : JsonConvert.SerializeObject(v).GetHashCode(),
v => JsonConvert.DeserializeObject<T>(JsonConvert.SerializeObject(v)));
propertyBuilder.HasConversion(converter);
propertyBuilder.Metadata.SetValueConverter(converter);
propertyBuilder.Metadata.SetValueComparer(comparer);
return propertyBuilder;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With