如何使我的字符串属性可为空? [英] How can I make my string property nullable?

查看:77
本文介绍了如何使我的字符串属性可为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使人的中间名为可选.我一直在使用C#.net代码优先方法.对于整数数据类型,只需使用?运算符使其可为空即可.我正在寻找一种使我的ing变量可为空的方法.我尝试搜索,但找不到使它可为空的方法.

I want to make the Middle Name of person optional. I have been using C#.net code first approach. For integer data type its easy just by using ? operator to make in nullable. I am looking for a way to make my sting variable nullable. I tried to search but could not find the way to make it nullable.

下面是我的代码.请建议我如何使其为可空值.

Below is my code. Please suggest me how to make it nullable.

public class ChildrenInfo
{
    [Key]
    public int ChidrenID { get; set; }

    [Required]
    [Display(Name ="First Name")]
    [StringLength(50,ErrorMessage ="First Name cannot exceed more than 50 characters")]
    [RegularExpression(@"^[A-Z]+[a-z]*$",ErrorMessage ="Name cannot have special character,numbers or space")]
    [Column("FName")]
    public string CFName { get; set; }

    [Display(Name ="Middle Name")]
    [RegularExpression(@"^[A-Z]+[a-z]*$",ErrorMessage ="Middle Name cannot have special character,numbers or space")]
    [StringLength(35,ErrorMessage ="Middle Name cannot have more than 35 characters")]
    [Column("MName")]
    public string? CMName { get; set; }
}   

推荐答案

字符串是引用类型,并且始终为空,您无需执行任何特殊操作.指定类型为可空值仅对于值类型是必需的.

String is a reference type and always nullable, you don't need to do anything special. Specifying that a type is nullable is necessary only for value types.

这篇关于如何使我的字符串属性可为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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