how to manage _id field when using POCO with mongodb c# driver

后端 未结 5 2058
面向向阳花
面向向阳花 2020-12-14 01:01

If I want to read and write mongo data with a POCO

public class Thingy
{
     public string Foo {get;set;}
}
...
coll.Insert(new Thing(Foo = \"hello\"));
         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 02:04

    You have to add a property (or field) for id and tell serializer which id generator you'd like to use.

    [BsonId(IdGenerator = typeof(ObjectIdGenerator))]
    public object ThingyId { get; set; }
    

    There are 3 available in MongoDb Driver or you can write your own. More info at http://www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutorial#CSharpDriverSerializationTutorial-WriteacustomIdgenerator

提交回复
热议问题