开发者

can i use pocos with mongodb c# driver

开发者 https://www.devze.com 2023-03-06 18:01 出处:网络
I was wondering if I can read and write raw pocos to mongodb the d开发者_运维问答river tutorial shows adding each field to a bsondocument one at a time. Does bsonserialzer do it?

I was wondering if I can read and write raw pocos to mongodb

the d开发者_运维问答river tutorial shows adding each field to a bsondocument one at a time. Does bsonserialzer do it?

I can write something myself to reflect on an object but I wonder it it already exists.

Working with dynamic expandos would be nice


Yes, the 10gen official C# MongoDB driver supports POCO serialisation and deserialisation, e.g.

MongoCollection<Thing> thingCollection = _db.GetCollection<Thing>("things");
Thing thing = col.FindAllAs<Thing>();
col.Insert(new Thing { Name = "Foo" });


I think you can and you should, 10gen driver POCO objects. You can design your POCO model in a completely sepparate assembly without any reference to Mongo.Driver o Mongo.BSon and configure the entry point of your app to use that assembly, setting indexes, ingnore fields, discriminators, ids columns, and a large etc.

 BsonClassMap.RegisterClassMap<Post>(cm =>
        {
            cm.AutoMap();
            cm.SetIdMember(cm.GetMemberMap(c => c.IdPost));
            cm.UnmapProperty(c => c.TimeStamp);
            cm.UnmapProperty(c => c.DatePostedFormat);
            cm.UnmapProperty(c => c.IdPostString);
            cm.UnmapProperty(c => c.ForumAvatar);
            cm.UnmapProperty(c => c.ForumAvatarAlt);
        });
0

精彩评论

暂无评论...
验证码 换一张
取 消