I am working a asp .net core project with mongoDb.
I am trying to get hotel by hotelCode.
HotelBedsContentService.cs
public async Task<HotelDocument> GetHotel(int code)
{
var x = await HotelCollection.Find(x => x.Code == code).FirstOrDefaultAsync();
return x;
}
When passing this line, I got below error.
await _hotelBedsContentService.GetHotel(hotel.HotelCode);
System.FormatException: An error occurred while deserializing the Destination property of class TechneTravel.Domain.Documents.HotelBedsContents.HotelDocument: An error occurred while deserializing the Id property of class TechneTravel.Domain.Documents.HotelBedsContents.DestinationDocument: Cannot deserialize a 'String' from BsonType 'ObjectId'.
---> System.FormatException: An error occurred while deserializing the Id property of class TechneTravel.Domain.Documents.HotelBedsContents.DestinationDocument: Cannot deserialize a 'String' from BsonType 'ObjectId'.
---> System.FormatException: Cannot deserialize a 'String' from BsonType 'ObjectId'.
at MongoDB.Bson.Serialization.Serializers.StringSerializer.DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
...........
HotelDocument.cs
public class HotelDocument
{
public string Id { get; set; }
}
Configuration.cs
BsonClassMap.RegisterClassMap<HotelDocument>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
cm.MapIdMember(c => c.Id).SetSerializer(new StringSerializer(BsonType.ObjectId)).SetIgnoreIfDefault(true);
});
Why this error occurs? Is there any mistake in my configuration?
Please help me find the issue