我有这样一个模型:
public int Id { get; set; }
[Required]
[StringLength(50, MinimumLength = 3)]
public string Title { get; set; }
[Required]
[StringLength(50, MinimumLength = 3)]
public string Slug { get; set; }
[Required]
[StringLength(100, MinimumLength = 3)]
public string Body { get; set; }
如您所见,对于 Body
,我将限制设置为 100,但在数据库中此列为 varchar(max)
。我可以输入什么来匹配列的 varchar(max)
长度?
最佳答案
你有两个选择:
(1) 使用 int.MaxLength
作为 StringLengthAttribute
构造函数的 maximumLength
参数:
[Required]
[StringLength(int.MaxValue, MinimumLength = 3)]
public string Body { get; set; }
(2) 使用不带参数的 MaxLengthAttribute
额外修饰属性(StringLengthAttribute
的值将被忽略):
[Required]
[StringLength(100, MinimumLength = 3)]
[MaxLength]
public string Body { get; set; }
关于c# - 指定最大长度属性以匹配 varchar(max),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40350157/