无法将BsonIgnoreIfDefault用于long类型的属性 [英] Unable to use BsonIgnoreIfDefault for property of type long

查看:101
本文介绍了无法将BsonIgnoreIfDefault用于long类型的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

属性 [BsonDefaultValue(0)] 阻止将整个文档插入到mongo中,而我只想阻止存储TotalItems(如果其值为零). 如果我不使用属性[BsonDefaultValue(0)],[BsonIgnoreIfDefault],则将文档正确插入到数据库中,并将TotalItems插入到文档中,作为"TotalItems":NumberLong(0)"实际上我不想在db为零的情况下进入数据库.我的问题是为什么[BsonDefaultValue(0)],[BsonIgnoreIfDefault]属性会阻止插入整个文档.

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.

注意:我可以将上述两个属性与int类型的属性一起使用.

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

推荐答案

此处存在轻微错误,您需要将默认值转换为long:

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

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

这篇关于无法将BsonIgnoreIfDefault用于long类型的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆