在使用CosoDB而不是在C#dot net中使用CosoDB时,ObjectID出现问题

时间:2017-10-11 19:08:09

标签: mongodb azure azure-cosmosdb

在CosmoDB中我有:

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

在C#.NET中,我有以下模型:

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

我收到以下错误:

"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()

1 个答案:

答案 0 :(得分:1)

_id在cosmos DB中自动设置。如果您不打算阅读它,请尝试从声明或声明本身中排除'set'(您可以随时介绍您自己的唯一ID,BTW,您可以在askcosmosdb@microsoft.com上预览唯一索引。 )