指定最大长度属性以匹配 varchar(max)
Specify max length attribute to match varchar(max)
我有这样一个模型:
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)
?
VARCHAR(MAX)
在 unicode check here.
中是 2^31-3
个字符
所以你可以只输入这个数字。但不知道你为什么需要这个 =) :
[StringLength(2147483645, MinimumLength = 3)]
你有两个选择:
(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; }
您可以使用 DataAnnotations.Schema
。我将以下内容用于 varchar(max)
[Column(TypeName = "varchar(MAX)")]
[MaxLength]
我有这样一个模型:
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)
?
VARCHAR(MAX)
在 unicode check here.
2^31-3
个字符
所以你可以只输入这个数字。但不知道你为什么需要这个 =) :
[StringLength(2147483645, MinimumLength = 3)]
你有两个选择:
(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; }
您可以使用 DataAnnotations.Schema
。我将以下内容用于 varchar(max)
[Column(TypeName = "varchar(MAX)")]
[MaxLength]