Specify the maximum length attribute to match varchar (max)

I have a model like this:

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; } 

As you can see for Body , I set the limit to 100, but in the database this column is varchar(max) . What can be put for length in correspondence with varchar(max) column?

+5
source share
3 answers

You have two options:

(1) Use int.MaxLength as the maximumLength argument of the StringLengthAttribute constructor:

 [Required] [StringLength(int.MaxValue, MinimumLength = 3)] public string Body { get; set; } 

(2) Decorate the property additionally with MaxLengthAttribute w / o parameters ( StringLengthAttribute value will be ignored):

 [Required] [StringLength(100, MinimumLength = 3)] [MaxLength] public string Body { get; set; } 
+5
source

VARCHAR(MAX) 2^31-3 chracters in unicode here> .

So you can just put this number. But I don’t know why you need it =):

 [StringLength(2147483645, MinimumLength = 3)] 
+1
source

You can use DataAnnotations.Schema. I use the following for varchar (max)

[Column (TypeName = "varchar (MAX)")] [MaxLength]

0
source

Source: https://habr.com/ru/post/1259068/


All Articles