无法应对A' String'来自bsontype' objectid'
我正在使用MongoDB进行ASP .NET核心项目。
我正在尝试通过酒店码来获得酒店。
hotelbedscontentservice.cs
public async Task<HotelDocument> GetHotel(int code)
{
var x = await HotelCollection.Find(x => x.Code == code).FirstOrDefaultAsync();
return x;
}
经过这条线时,我的错误低于错误。 等待_hotelbedscontentservice.gethotel(hotel.hotelcode);
System.FormatException: An error occurred while deserializing the Destination property of class TechneTravel.Domain.Documents.HotelBedsContents.HotelDocument: An error occurred while deserializing the Id property of class TechneTravel.Domain.Documents.HotelBedsContents.DestinationDocument: Cannot deserialize a 'String' from BsonType 'ObjectId'.
---> System.FormatException: An error occurred while deserializing the Id property of class TechneTravel.Domain.Documents.HotelBedsContents.DestinationDocument: Cannot deserialize a 'String' from BsonType 'ObjectId'.
---> System.FormatException: Cannot deserialize a 'String' from BsonType 'ObjectId'.
at MongoDB.Bson.Serialization.Serializers.StringSerializer.DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
...........
hoteldocument.cs
public class HotelDocument
{
public string Id { get; set; }
}
configuration.cs
BsonClassMap.RegisterClassMap<HotelDocument>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
cm.MapIdMember(c => c.Id).SetSerializer(new StringSerializer(BsonType.ObjectId)).SetIgnoreIfDefault(true);
});
为什么会发生此错误?我的配置有任何错误吗?
请帮助我找到问题
I am working a asp .net core project with mongoDb.
I am trying to get hotel by hotelCode.
HotelBedsContentService.cs
public async Task<HotelDocument> GetHotel(int code)
{
var x = await HotelCollection.Find(x => x.Code == code).FirstOrDefaultAsync();
return x;
}
When passing this line, I got below error.await _hotelBedsContentService.GetHotel(hotel.HotelCode);
System.FormatException: An error occurred while deserializing the Destination property of class TechneTravel.Domain.Documents.HotelBedsContents.HotelDocument: An error occurred while deserializing the Id property of class TechneTravel.Domain.Documents.HotelBedsContents.DestinationDocument: Cannot deserialize a 'String' from BsonType 'ObjectId'.
---> System.FormatException: An error occurred while deserializing the Id property of class TechneTravel.Domain.Documents.HotelBedsContents.DestinationDocument: Cannot deserialize a 'String' from BsonType 'ObjectId'.
---> System.FormatException: Cannot deserialize a 'String' from BsonType 'ObjectId'.
at MongoDB.Bson.Serialization.Serializers.StringSerializer.DeserializeValue(BsonDeserializationContext context, BsonDeserializationArgs args)
...........
HotelDocument.cs
public class HotelDocument
{
public string Id { get; set; }
}
Configuration.cs
BsonClassMap.RegisterClassMap<HotelDocument>(cm =>
{
cm.AutoMap();
cm.SetIgnoreExtraElements(true);
cm.MapIdMember(c => c.Id).SetSerializer(new StringSerializer(BsonType.ObjectId)).SetIgnoreIfDefault(true);
});
Why this error occurs? Is there any mistake in my configuration?
Please help me find the issue
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
收藏集中的某些
_id
看起来无法转换为ObjectID。以下代码效果很好:
请提供触发您的问题的数据示例
It looks like some
_id
in your collection cannot be converted to ObjectId.The below code works well:
please provide a data sample that trigger your issue
MongoDB仅在ObjectID类型上插入时处理自动ID生成。
如果要使用字符串或int或长时间,则需要在插入之前设置ID。
之类的东西检查数据一致性
您可以使用 https://github.com/iso8859/learn-mongodb-by-example/blob/main/main/dotnet/02%20-%20-%20-intermediate/checkidconsistency.cs
MongoDB handles automatic id generation on Insert for ObjectId type only.
If you want to use string or int or long then you need to set the id before you insert.
You can check your data consistency with something like
https://github.com/iso8859/learn-mongodb-by-example/blob/main/dotnet/02%20-%20Intermediate/CheckIdConsistency.cs