Cannot deserialize string from BsonType ObjectId in MongoDb C#

前端 未结 2 1532
误落风尘
误落风尘 2021-02-18 22:31

I am getting error \"Cannot deserialize string from BsonType ObjectId\" while trying to get all the record from MongoDb in C# WebAPI

My Id is



        
相关标签:
2条回答
  • 2021-02-18 23:02

    Guid.NewGuid() will not produce ObjectId. Object Id is 12 byte data structure and Guid produce 16byte hex string (without '-')

    You should remove attribute [BsonRepresentation(BsonType.ObjectId)]

    You can use any string as Id in your entity for example 'HiDude' and any string in utf8 format.

    0 讨论(0)
  • 2021-02-18 23:05

    Instead of using

    ed.Id = Guid.NewGuid().ToString();
    

    I used

    ed.Id = MongoDB.Bson.ObjectId.GenerateNewId().ToString();
    

    For generating Id

    Its working fine : )

    0 讨论(0)
提交回复
热议问题