开发者

Mongo C# ignore property

I'm using v0.9 of the official MongoDB driver and i'm trying to read in a collection. I have a field in the database that I don't want to read into my object but I get the following error.

"Unexpected element: Network"

The collection looks like this in the database

Merchants
 开发者_高级运维- _id
 - Name
 - Description
 - Url
 - Network

When I read it into C# I want to create an object called Merchant that has all of the same properties, except "Network". How do I do this?


There's an "IgnoreExtraElements" option on the BSON serializer which you can enable to prevent that error.

Either set it as an attribute on your Merchant class:

[BsonIgnoreExtraElements]
public Merchant {
    // fields and properties
}

or in code if you're using class maps:

BsonClassMap.RegisterClassMap<Merchant>(cm => {
    cm.AutoMap();
    cm.SetIgnoreExtraElements(true);
});
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜