简体   繁体   中英

can i use pocos with mongodb c# driver

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

the driver 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, eg

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);
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM