我有一个这样的模型:
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 w / o参数另外装饰属性(将忽略StringLengthAttribute的值):
[required] [StringLength(100,MinimumLength = 3)] [MaxLength] public string Body { get; set; }