I have set default values. I've created a custom default value
convention that returns Guid.Empty for all Guids and ObjectId.Empty
for all object IDs. It in combination with manually doing the class
maps is the only way I've been able to prevent them from being saved.
If I take out my CustomDefaultValueConvention or the manual class
maps, the values get saved. I was really hoping that my
CustomDefaultValueConvention in combination with the provided
NeverSerializeDefaultValueConvention would work so that I don't have
to manually map every single field I want to prevent defaults for.
Below is an example of what is working. I'll get the source and look
through it and the unit tests to see why it isn't doing what I think
it should when I have a little more time to devote to it.
var conventions = new ConventionProfile();
conventions.SetDefaultValueConvention(new
CustomDefaultValueConvention());
conventions.SetSerializeDefaultValueConvention(new
NeverSerializeDefaultValueConvention());
conventions.SetIgnoreExtraElementsConvention(new
AlwaysIgnoreExtraElementsConvention());
conventions.SetIgnoreIfNullConvention(new
AlwaysIgnoreIfNullConvention());
BsonClassMap.RegisterConventions(conventions, t => t.FullName != null
&& t.FullName.StartsWith("Asi.AccuAuto"));
BsonClassMap.RegisterClassMap<Policy>(cm =>
{
cm.AutoMap();
cm.GetMemberMap(c =>
c.CommissionId).SetSerializeDefaultValue(false);
cm.GetMemberMap(c =>
c.PriorPolicyId).SetSerializeDefaultValue(false);
cm.GetMemberMap(c =>
c.ManagementCompanyId).SetSerializeDefaultValue(false);
});
On Aug 23, 5:54 pm, sridhar <
srid...@10gen.com> wrote:
> NeverSerializeDefaultValueConvention is used in conjunction with
> BsonDefaultValue. If you specify a default value for a field, you then
> use the NeverSerializeDefaultValueConvention to not serialize it. An
> example of this can be found in the unit tests for the driver at
> BsonUnitTests\DefaultSerializer\Attributes\BsonAttributeTests.cs
>
> If you want attributes to not be serialized then tag the field/
> property using BsonIgnore as specified inhttp://
www.mongodb.org/display/DOCS/CSharp+Driver+Serialization+Tutor....