如果为null或为空,则不要插入字段 [英] Do not insert field if null or empty

查看:112
本文介绍了如果为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.

推荐答案

使用恰当命名的

Using the aptly named [BsonIgnoreIfNull] attribute:

class User
{
    public string FirstName;
    public string LastName;
    [BsonIgnoreIfNull]
    public string MidName;
}

这篇关于如果为null或为空,则不要插入字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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