2016-08-04 110 views
1

在EF6中,是否存在允许空字符串但不允许为空的注释?EF6 - 注释允许空字符串,但不允许为null

我试过[必须]。但是这个注解不允许空字符串或null。

[Required] 
[MaxLength(80)] 
public string ShortDescription { get; set; } 
+0

背后的原因是什么? –

+0

在大多数情况下,我很少或没有理由将空值置于数据库中。检查空字符串就像检查null一样好。在大多数情况下,我没有看到检查两者的理由。 –

回答

1

看起来我只需要将(AllowEmptyStrings = true)选项添加到[Required]注释。

[Required(AllowEmptyStrings = true)] 
[MaxLength(80)] 
public string ShortDescription { get; set; }