SerializerRegistry is ignored while using EF Core

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 per contextl:

cb.Properties<Instant>()
   .HaveConversion(i => i.Ticks, l => new Instant(l));

You can learn more about Value Converters in the EF Core docs at

1 Like