0

I create simple class:

 public class User
{       
  //  [BsonRepresentation(BsonType.ObjectId)]
    public ObjectId _id { get; set; }

    [BsonRepresentation(BsonType.String)]
    public String Name { get; set; }

    [BsonRepresentation(BsonType.String)]
    public string Email { get; set; }
}

And then i try to get users collection:

  using (var cursor = await col.Find<User>(Builders<User>.Filter.Empty).ToCursorAsync())
            {
                while (cursor.MoveNext())
                {
                   //do work
                }
            }

So, it thows exception:"Cannot deserialize a 'ObjectId' from BsonType 'Int32'" at line "ToCursorAsync()".

What i am doing wrong?

Ka Res
  • 311
  • 2
  • 3
  • 17
Admiral Land
  • 2,304
  • 7
  • 43
  • 81
  • Most likely a decoration issue. See Link: https://stackoverflow.com/questions/38871416/difference-between-decorating-a-property-in-c-sharp-with-bsonrepresentationbson – Brandon Amador Jan 18 '18 at 22:36
  • It's likely you have a document in your collection that has a numeric `_id` rather than an `ObjectId`. – Kirk Larkin Jan 19 '18 at 00:06

0 Answers0