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)
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.
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.
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...
Data Type. Included columns can be varchar (max), nvarchar(max) , varbinary(max) or XML data types, that you cannot add it as index keys.
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.
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