I am using NodaTime package for date & time manipulation and it seems I cannot make it work with Mongo EF Core package. I was trying to follow Serialization documentation and I found only sealed class of BsonSerializerFactory.
Exception: No known serializer for type 'Instant'.
at MongoDB.EntityFrameworkCore.Serializers.BsonSerializerFactory.CreateTypeSerializer(Type type, IReadOnlyProperty property)
at MongoDB.EntityFrameworkCore.Serializers.BsonSerializerFactory.CreateTypeSerializer(IReadOnlyProperty property)
at MongoDB.EntityFrameworkCore.Serializers.BsonSerializerFactory.GetPropertySerializationInfo(IReadOnlyProperty property)
at MongoDB.EntityFrameworkCore.Storage.MongoUpdate.WriteProperty(IBsonWriter writer, Object value, IProperty property)
How can I register custom BSON serializers for EF Core mongo provider?
Hi.
The docs on serializers are for when using the MongoDB C# Driver directly and while the MongoDB EF Core Provider uses the MongoDB C# Driver and BsonSerializers behind the scenes it’s not possible to add your own as EF Core requires more granular control over the process.
Instead you can use the EF Core HasConversion method available on the property builder in order to control how CLR types are serialized and deserialized.
e.g. if you have a Event class that has a property with When of type Instant and you want to store it as an long in the database you could in your EF model builder code:
mb.Entity<Event>()
.Property(e => e.When)
.HasConversion(i => i.Ticks, l => new Instant(l));
If you’d like to do this at a type level rather than per-property you can use the EF config builder to specify it at a more global level:
cb.Properties<Instant>()
.HaveConversion(i => i.Ticks, l => new Instant(l));
You can learn more about Value Converters in the EF Core docs at