I've been searching around the web trying to figure out the right syntax to have Entity Framework Code First create my table with a column: varchar(max).
This is what I have. By default this creates varchar(128). How do I create varchar(max)?
I have tried [MaxLength] without success.
Any help would be appreciated. Thanks!
[Column(TypeName = "varchar")] public string MediaDesc { get; set; }
you need to include the MaxLength attribute [Column(TypeName = "varchar(MAX)"), MaxLength] . If you don't, you will get varchar(MAX) in the database but validation will throw for more than 100 characters.
So SQL Server is storing normal VARCHAR(n) columns and VARCHAR(MAX) columns “in row” by default. When VARCHAR(MAX) exceeds 8,000 characters, the pointer is stored “in row”, and the string is stored in “LOB” pages.
varchar [ ( n | max ) ] Variable-size string data. Use n to define the string size in bytes and can be a value from 1 through 8,000 or use max to indicate a column constraint size up to a maximum storage of 2^31-1 bytes (2 GB).
The key difference between varchar and nvarchar is the way they are stored, varchar is stored as regular 8-bit data(1 byte per character) and nvarchar stores data at 2 bytes per character. Due to this reason, nvarchar can hold upto 4000 characters and it takes double the space as SQL varchar.
[Column(TypeName = "varchar(MAX)")]
Surprisingly the most obvious solution works.
The [MaxLength]
attribute only creates a varchar
column with a max length that isn't MAX but - in my case (SQL Server Express 2008 R2) - 8000.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With