如果为null或为空,则不要插入字段

编程入门 行业动态 更新时间:2024-10-28 07:33:20
本文介绍了如果为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或为空,则不要插入字段

本文发布于:2023-10-20 09:16:52,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1510560.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   为空   null

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!