Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement for all classes BsonIgnoreExtraElements

Tags:

I'm using mongDb with MongoDrive, I wonder how I can implement to all my classes the [BsonIgnoreExtraElements].

I know there is a way through the ConventionProfile, but I do not know how to implement it.

like image 319
Cleyton Ferrari Avatar asked Oct 17 '12 22:10

Cleyton Ferrari


1 Answers

Edit

Per Evereq's comment, the below is obsolete. Now use:

var conventionPack = new ConventionPack { new IgnoreExtraElementsConvention(true) }; ConventionRegistry.Register("IgnoreExtraElements", conventionPack, type => true); 

Use the SetIgnoreExtraElementsConvention method (from the Conventions section of the C# Driver Serialization Tutorial):

var myConventions = new ConventionProfile(); myConventions.SetIgnoreExtraElementsConvention(new AlwaysIgnoreExtraElementsConvention())); BsonClassMap.RegisterConventions(myConventions, (type) => true); 

The parameter (type) => true is a predicate depending on the class type, that determines whether to apply the convention. So per your requirement it should simply return true regardless; but you could use this to set/exclude the convention on given types if you wanted.

like image 183
McGarnagle Avatar answered Sep 19 '22 13:09

McGarnagle