简体   繁体   中英

MongoDB C#: Cannot deserialize a 'String' from BsonType 'DateTime'

I am using Azure CosmosDB's Mongo API in my C# .NET Core app, using the MongoDB driver.

I have a class which contains the following:

class MyModel
{
 [BsonElement("mydate")]
 public string MyDate { get; set; }
}

and when I try to retrieve the model (the table contains a DateTime ), I get the following error:

Cannot deserialize a 'String' from BsonType 'DateTime'.

I have tried doing this:

 [BsonElement("mydate")]
 [BsonRepresentation(BsonType.DateTime)]
 public string MyDate { get; set; }

but then I got this error:

DateTime is not a valid representation for a StringSerializer.

Did you try to convert it to

class MyModel
{
 [BsonElement("mydate")]
 public DateTime MyDate { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM