Trouble with ObjectID when taking MongoDB to use CosmoDB instead in C# dot net

0

In CosmoDB I have:

{
"_id" : ObjectId("59de66a5e0f53d0d78e3d176"),
"id" : "replace_with_new_document_id",
"name" : "patientx"
}

In C# .NET I've got the following model:

public class Patient_test
{
    public ObjectId _id { get; set; }
    public string name { get; set; }
    public string id { get; set; }
}

I get the following error:

"MongoDB.Bson.BsonSerializationException occurred
  HResult=0x80131500
  Message=The property 'id' of type 'EMR.Models.Patient.Patient_test' cannot use element name '_id' because it is already being used by property '_id'.
  Source=<Cannot evaluate the exception source>
  StackTrace:
   at MongoDB.Bson.Serialization.BsonClassMap.Freeze()
   at MongoDB.Bson.Serialization.BsonClassMap.LookupClassMap(Type classType)
   at MongoDB.Bson.Serialization.BsonClassMapSerializationProvider.GetSerializer(Type type, IBsonSerializerRegistry serializerRegistry)
   at MongoDB.Bson.Serialization.BsonSerializerRegistry.CreateSerializer(Type type)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at MongoDB.Bson.Serialization.BsonSerializerRegistry.GetSerializer(Type type)
   at MongoDB.Bson.Serialization.BsonSerializer.LookupSerializer[T]()
   at MongoDB.Bson.Serialization.BsonSerializer.Deserialize[TNominalType](IBsonReader bsonReader, Action`1 configurator)
   at MongoDB.Bson.Serialization.BsonSerializer.Deserialize[TNominalType](BsonDocument document, Action`1 configurator)
   at EMR.Controllers.PatientController.Index(Patient p) in C:\Users\devin\Desktop\SE\WK8DELIVERABLE\EMR\EMR\Controllers\PatientController.cs:line 35
   at Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker.<InvokeActionMethodAsync>d__27.MoveNext()
mongodb
azure
azure-cosmosdb
asked on Stack Overflow Oct 11, 2017 by Devin Andres Salemi • edited Oct 12, 2017 by David Makogon

1 Answer

1

_id is set automatically in cosmos DB. Try to exclude ‘set’ from declaration or the declaration itself if you don’t intentd o read it (you can always introduce your own unique id, BTW, there’re preview of unique indexes that you can request at askcosmosdb@microsoft.com)

answered on Stack Overflow Oct 11, 2017 by alekseys

User contributions licensed under CC BY-SA 3.0