Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework uses nvarchar (max) when MaxLength is specified

I have specified a text field's MaxLength as 4096 with EF fluent api in order to limit its length:

this.Property(p => p.MyText).HasMaxLength(4096).IsRequired();

But for some reason in SQL Server, the column becomes nvarchar (max).

Just for test if I specify 2048 to make sure that SQL Server gets updated

this.Property(p => p.MyText).HasMaxLength(2048).IsRequired();

And this way it is works.

So my queston why EF sets sql nvarchar (max) when MaxLength(4096)

like image 672
sreginogemoh Avatar asked Aug 27 '15 13:08

sreginogemoh


People also ask

When should I use nvarchar Max?

Use varchar when the sizes of the column data entries vary considerably. Use varchar(max) when the sizes of the column data entries vary considerably, and the size might exceed 8,000 bytes.

How much memory does nvarchar max use?

nvarchar [ ( n | max ) ] max indicates that the maximum storage size is 2^30-1 characters (2 GB). The storage size is two times n bytes + 2 bytes. For UCS-2 encoding, the storage size is two times n bytes + 2 bytes and the number of characters that can be stored is also n.

What is the difference between nvarchar 50 and nvarchar Max?

nvarchar max is for columns up to 2GB. So essentially it takes up more resources. You are better off using the nvarchar(50) if you know you aren't going to need that much space. each character is about 2 bytes so with 2 GB thats 1 billion characters...

Can you index nvarchar Max?

Data Type. Included columns can be varchar (max), nvarchar(max) , varbinary(max) or XML data types, that you cannot add it as index keys.


1 Answers

I think it's SQL Server limits. msdn says

Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000.

like image 110
Denis Avatar answered Sep 28 '22 01:09

Denis