MongoDB C# Driver: Ignore Property on Insert

前端 未结 5 1652
余生分开走
余生分开走 2021-02-03 18:48

I am using the Official MongoDB C# Drive v0.9.1.26831, but I was wondering given a POCO class, is there anyway to ignore certain properties from getting inserted.

For ex

5条回答
  •  醉话见心
    2021-02-03 19:20

    Just in case somebody might be interested in another way of doing it. Via conventions:

    public class IgnoreSomePropertyConvention : ConventionBase, IMemberMapConvention
    {
        public void Apply(BsonMemberMap memberMap)
        { // more checks will go here for the case above, e.g. type check
            if (memberMap.MemberName != "DoNotWantToSaveThis")
                memberMap.SetShouldSerializeMethod(o => false);
        }
    }
    

    And then you need to register this convention once during you app startup:

    ConventionRegistry.Register("MyConventions", new ConventionPack { new IgnoreBaseIdConvention()  }, t => true);
    

提交回复
热议问题