Efcore: HasMaxLength(4000) set to NVARCHAR(MAX)

Created on 2 Jun 2017  路  11Comments  路  Source: dotnet/efcore

Steps to reproduce

I have the following API:
builder.Entity() .Property(p => p.Message).IsRequired().HasMaxLength(4000);

However, SQL Server uses NVARCHAR(MAX) instead of NVARCHAR(4000).

Further technical details

EF Core version: 1.1.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer 1.1.2
Operating system: Windows 7
IDE: Visual Studio Community 2017

closed-fixed type-bug

Most helpful comment

"Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000"
In SQL you can DECLARE @_string AS NVARCHAR(4000)
So, I don't think nvarchar(4000) should be equivalent of nvarchar(max) is correct.

All 11 comments

Note that the Migration's Up() method correctly generate the following codes:
migrationBuilder.AddColumn(
name: "Message",
table: "EventLogs",
maxLength: 4000,
nullable: false);

Can you explain what do you mean by this?

However, SQL Server uses NVARCHAR(MAX) instead of NVARCHAR(4000).

What I meant is that the datatype is NVARCHAR(MAX) instead of NVARCHAR(4000).

Can you also post output of Script-Migration (dotnet ef migration script if using dotnet cli)

That's a good idea, let me take a look.

  1. Fluent API
    8685_1

  2. Migration
    8685_2

  3. SQL
    8685_3

The above screenshots should describe the problem. Thanks.

https://docs.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql

~nvarchar(4000) should be equivalent of nvarchar(max)~
nvarchar(4001) should be equivalent of nvarchar(max). Error of 1.

"Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000"
In SQL you can DECLARE @_string AS NVARCHAR(4000)
So, I don't think nvarchar(4000) should be equivalent of nvarchar(max) is correct.

Off by one.

This no longer repros on dev. Somebody must have inadvertently fixed it. 馃槈

https://github.com/aspnet/EntityFramework/commit/ae2277ed90c782500df29d2bf735491f316da9a8
Which removed the always setting size for parameters of string type. Instead null are now allowed which makes them nvarchar(max) again.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

vsfeedback picture vsfeedback  路  98Comments

econner20 picture econner20  路  97Comments

0xdeafcafe picture 0xdeafcafe  路  189Comments

satyajit-behera picture satyajit-behera  路  275Comments

matteocontrini picture matteocontrini  路  88Comments