Specify max length attribute to match varchar(max)

asp.net-mvc c# entity-framework

Question

I have a model like so:

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 the Body, I set a limit of 100, but in the database this column is varchar(max). What can I put for length to match the column's varchar(max) ?

1
11
10/31/2016 8:44:57 PM

Accepted Answer

You have two options:

(1) Use int.MaxLength as 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 (the value of the StringLengthAttribute will be ignored):

[Required]
[StringLength(100, MinimumLength = 3)]
[MaxLength]
public string Body { get; set; }
11
10/31/2016 9:21:14 PM

Popular Answer

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

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



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow