Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is new way of setting DateTimeSerializationOptions.Defaults in mongodb c# driver?

I was using this line to set the datetime defaults.

DateTimeSerializationOptions.Defaults = DateTimeSerializationOptions.LocalInstance;

I get this warning. 'MongoDB.Bson.Serialization.Options.DateTimeSerializationOptions.Defaults' is obsolete: 'Create and register a DateTimeSerializer with the desired options instead.'

But i couldn't find an example to change it... how can I change this obsolute usage?

like image 932
Serdar Avatar asked Oct 21 '22 11:10

Serdar


1 Answers

Register the date/time serializer like this:

BsonSerializer.RegisterSerializer(typeof(DateTime), DateTimeSerializer.LocalInstance);

Note that you can't register a serializer once one has already been registered. Also, the driver creates a default serializer for each type the first time it needs one. So you need to call this code before you make your first call to the driver to read or write data.

like image 177
Paul Shealy Avatar answered Nov 15 '22 07:11

Paul Shealy