本文介绍了如果为null或为空,则不要插入字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个C#类,其中包含一些字段,其中某些字段为null.那些为空的我不想插入具有空值的数据库.我根本不希望它们插入db中. 我该如何实现?
I have a C# class with some fields and some of them are null. Those that are null I do not want to be inserted into db with null value. I do not want them inserted into db at all. How do I achieve that?
class User { public string FirstName; public string LastName; public string MidName; }不是每个用户都有一个MidName,但是Mongo会在MidName字段中以空值插入db.
Not every user has a MidName, but Mongo inserts into db with null value on MidName field.
推荐答案使用恰当命名的 [BsonIgnoreIfNull]属性:
Using the aptly named [BsonIgnoreIfNull] attribute:
class User { public string FirstName; public string LastName; [BsonIgnoreIfNull] public string MidName; }更多推荐
如果为null或为空,则不要插入字段
发布评论