Sergino
Sergino

Reputation: 10838

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)

Upvotes: 7

Views: 9525

Answers (1)

Denis
Denis

Reputation: 6082

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.

Upvotes: 8

Related Questions