System.FormatException: 元素 'HoleId' 不匹配 class build.Models.Product 的任何字段或 属性
System.FormatException: Element 'HoleId' does not match any field or property of class build.Models.Product
我是 C# 新手。我正在尝试使用 MongoAPI Web 驱动程序在 C# 中连接 COSMOS DB。我正在关注他们的 documentation 并尝试从我的 collection.
获取数据
在文档中,他们给出了示例 Templates/APIForMongoDBQuickstart-WebAPI
最初,根据他们的示例数据 collection,我将此文档上传到我的 collection 并且我能够 运行 从获取产品 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
}
当我尝试在同一文档中添加新的 ObjectId 字段时,出现以下错误。
新字段:"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)
根据一些论坛指南,我在 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; }
}
}
即使我修改了代码,我仍然遇到同样的错误。谁能告诉我我卡在哪里了?我该如何纠正错误?
看起来是一般性问题,与模板无关。好的,如果您注意到控制台上的实际问题,
MongoDB.Bson.DuplicateBsonMemberMapAttributeException: Attributes of
type MongoDB.Bson.Serialization.Attributes.BsonIdAttribute can only be
applied to a single member.
这意味着您可以通过设置 (BsonType.ObjectId)
来消除该错误,应该有效
[BsonRepresentation(BsonType.ObjectId)]
public string HoleId { get; set; }
我是 C# 新手。我正在尝试使用 MongoAPI Web 驱动程序在 C# 中连接 COSMOS DB。我正在关注他们的 documentation 并尝试从我的 collection.
获取数据在文档中,他们给出了示例 Templates/APIForMongoDBQuickstart-WebAPI
最初,根据他们的示例数据 collection,我将此文档上传到我的 collection 并且我能够 运行 从获取产品 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
}
当我尝试在同一文档中添加新的 ObjectId 字段时,出现以下错误。
新字段:"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)
根据一些论坛指南,我在 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; }
}
}
即使我修改了代码,我仍然遇到同样的错误。谁能告诉我我卡在哪里了?我该如何纠正错误?
看起来是一般性问题,与模板无关。好的,如果您注意到控制台上的实际问题,
MongoDB.Bson.DuplicateBsonMemberMapAttributeException: Attributes of type MongoDB.Bson.Serialization.Attributes.BsonIdAttribute can only be applied to a single member.
这意味着您可以通过设置 (BsonType.ObjectId)
来消除该错误,应该有效
[BsonRepresentation(BsonType.ObjectId)]
public string HoleId { get; set; }