【发布时间】:2015-12-27 11:52:46
【问题描述】:
我想将人的中间名 (CMName) 设为可选。我一直在使用 C#.net 代码优先方法。对于整数数据类型,只需使用? 运算符即可轻松设置为空。我正在寻找一种使我的 sting 变量可以为空的方法。我试图搜索,但找不到使其可以为空的方法。
下面是我的代码。请建议我如何使它可以为空。
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; }
}
【问题讨论】:
-
是时候复习 C# 8.0 的答案并调整正确的答案了。
标签: c# asp.net-mvc entity-framework entity-framework-migrations