Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a column in SQL Server to varchar(max) using ASP.net EF Codefirst Data Annotations?

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; } 
like image 288
Maddhacker24 Avatar asked Jun 27 '12 22:06

Maddhacker24


People also ask

How to set VARCHAR max in entity framework?

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.

How is VARCHAR max stored SQL Server?

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.

Can Max be used on VARCHAR?

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).

What is the difference between VARCHAR and nvarchar?

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.


1 Answers

[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.

like image 91
Slauma Avatar answered Sep 23 '22 07:09

Slauma