2
votes

I am new to C#. I am trying to connect COSMOS DB in C# using MongoAPI web driver. I am following their documentation and trying to get data from my collection.

In the documentation, they gave sample Templates/APIForMongoDBQuickstart-WebAPI

Initially, by their sample data collection, I uploaded this document into my collection and I am able to run the get Products from getting Product API.

{
    "_id" : ObjectId("611a8420fe05b73300a7cae4"),
    "CategoryName" : "Components, Saddles",
    "Sku" : "SE-R581",
    "Name" : "LL Road Seat/Saddle",
    "Description" : "The product called \"LL Road Seat/Saddle\"",
    "Price" : 27.12
}

When I try to add a new ObjectId field in the same document, I am getting the following error. New Field: "HoleId" : ObjectId("51e0373c6f35bd826f47e9a5"),

System.FormatException: Element 'HoleId' does not match any field or property of class build.Models.Product.
   at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeClass(BsonDeserializationContext context)
   at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
   at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
   at MongoDB.Driver.Core.Operations.CursorBatchDeserializationHelper.DeserializeBatch[TDocument](RawBsonArray batch, IBsonSerializer`1 documentSerializer, MessageEncoderSettings messageEncoderSettings)

As per some forum guidelines, I done following changes in azure-cosmos-dotnet-templates/Templates/APIForMongoDBQuickstart-WebAPI/Models/Product.cs

namespace APIForMongoDBQuickstart_WebAPI.Models
{
    public class Product
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        [JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingDefault)]
        public string Id { get; set; }

        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string HoleId { get; set; } // My modified code here

        public string CategoryName { get; set; }
        public string Sku { get; set; }
        public string Name { get; set; }
        public string Description { get; set; }
        public double Price { get; set; }
    }
}

Even though I modified the code, Still I am getting the same error. Could anyone tell me where I got stuck? How can I rectify the bug?

1
you added HoleId to existing data?Sajeetharan
@Sajeetharan: Yes.. After updating the data, { "_id" : ObjectId("611a8420fe05b73300a7cae4"), "HoleId": ObjectId("51e0373c6f35bd826f47e9a5"), "CategoryName" : "Components, Saddles", "Sku" : "SE-R581", "Name" : "LL Road Seat/Saddle", "Description" : "The product called \"LL Road Seat/Saddle\"", "Price" : 27.12 }Smith Dwayne

1 Answers

2
votes

It looks like generic issue, not related to the template. Ok the actual issue if you notice on the consolse,

MongoDB.Bson.DuplicateBsonMemberMapAttributeException: Attributes of type MongoDB.Bson.Serialization.Attributes.BsonIdAttribute can only be applied to a single member.

which means you can get rid of that error by having (BsonType.ObjectId), should work

 [BsonRepresentation(BsonType.ObjectId)]
 public string HoleId { get; set; }