System.FormatException: An error occurred while deserializing the XXX property of class ADocument+BDocument: Unknown discriminator value 'JsonElement'

Viewed 1063

I am wokring a asp.net core project with mongoDB.

I want to get the list of booked hotels from HotelBookingDocument.

public class HotelBookingDocument
{
    public string Id { get; set; }
    public HotelDocument Hotel { get; set; }
// removed rest 
}

public class HotelDocument
{
    public List<RoomDocument> Rooms { get; set; }
// removed rest 
}

public class RoomDocument
{
      public object SupplierReference { get; set; }
// removed rest 
}
// removed rest 

Configuration

BsonClassMap.RegisterClassMap<HotelBookingDocument>(cm =>
{
  cm.AutoMap();
  cm.SetIgnoreExtraElements(true);
  cm.MapIdMember(c => c.Id)
    .SetSerializer(new StringSerializer(BsonType.ObjectId))
    .SetIgnoreIfDefault(true);
});

BsonClassMap.RegisterClassMap<HotelBookingDocument.RoomDocument>(cm =>
{
  cm.AutoMap();
});
            BsonClassMap.RegisterClassMap<HotelBookingDocument.HotelDocument>(cm =>
{
  cm.AutoMap();
});

Query

public async Task<List<HotelBookingDocument>> GetHotelsList(string transactionId)
{
  var hotels = await HotelBookingCollection.Find(x => x.TransactionId == transactionId).ToListAsync();   // this does not work
  return hotels;
}

Screenshot of SupplierReference Property in Database

Screenshot of SupplierReference Property in Database

Why this "_t": "JsonElement" did add automatically?

"SupplierReference": {
                "_t": "JsonElement"
            },

Error:

System.FormatException: An error occurred while deserializing the Hotel property of class HotelBookingDocument: An error occurred while deserializing the Rooms property of class HotelBookingDocument+HotelDocument: An error occurred while deserializing the SupplierReference property of class HotelBookingDocument+RoomDocument: Unknown discriminator value 'JsonElement'.
---> System.FormatException: An error occurred while deserializing the Rooms property of class HotelBookingDocument+HotelDocument: An error occurred while deserializing the SupplierReference property of class HotelBookingDocument+RoomDocument: Unknown discriminator value 'JsonElement'.
---> System.FormatException: An error occurred while deserializing the SupplierReference property of class HotelBookingDocument+RoomDocument: Unknown discriminator value 'JsonElement'.
---> MongoDB.Bson.BsonSerializationException: Unknown discriminator value 'JsonElement'.
 at MongoDB.Bson.Serialization.BsonSerializer.LookupActualType(Type nominalType, BsonValue discriminator)
 at MongoDB.Bson.Serialization.Conventions.ObjectDiscriminatorConvention.GetActualType(IBsonReader bsonReader, Type nominalType)
 at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args)
 at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
 at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)...............

when I try to get list of hotelBookings from HotelBookingCollection by transactionId, I got above error. But I find a hotelBooking from HotelBookingCollection i did not get any error. I can get first hotelBooking.

Why this error occur?

Please help me to find the issue.

Thank you

0 Answers
Related