Unable to use BsonIgnoreIfDefault for property of type long

巧了我就是萌 提交于 2019-12-11 09:31:38

问题


[BsonDefaultValue(0)]
[BsonIgnoreIfDefault]
public long TotalItems { get; set; }

The attribute [BsonDefaultValue(0)] is preventing the complete document being inserted into the mongo whereas I just want to prevent storing TotalItems if its value is zero. If I don't use the attributes [BsonDefaultValue(0)], [BsonIgnoreIfDefault] the the document is inserted properly in the db with TotalItems is inserted in the document as "TotalItems" : NumberLong(0)" which I actually don't want to strore into db if its zero. My question why [BsonDefaultValue(0)], [BsonIgnoreIfDefault] attributes preventing the insertion of the complete document.

Note: I am able to use the above two attributes with properties of type int.


回答1:


Minor error there, you need to cast your default value to a long:

[BsonDefaultValue((long)0)]
[BsonIgnoreIfDefault]
public long TotalItems { get; set; }


来源:https://stackoverflow.com/questions/26601732/unable-to-use-bsonignoreifdefault-for-property-of-type-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!