question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Scaffolding broken for nvarchar(4000)

See original GitHub issue

When using SQL Server and a table has a column type of nvarchar(4000) (which is the maximum), the scaffolding outputs .HasColumnType(“nvarchar”). This causes an exception when accessing the context for the model.

Believe it’s a bug in the scaffolding code.

Exception message: System.ArgumentException: Data type 'nvarchar' is not supported in this form. Either specify the length explicitly in the type name, for example as 'nvarchar(16)', or remove the data type and use APIs such as HasMaxLength to allow EF choose the data type. 
Stack trace: at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerTypeMapper.ValidateTypeName(String storeType)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.ValidateDataTypes(IModel model)
   at Microsoft.EntityFrameworkCore.Infrastructure.RelationalModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Internal.SqlServerModelValidator.Validate(IModel model)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.<>c__DisplayClass5_0.<GetModel>b__0(Object k)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.GetModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
   at Microsoft.EntityFrameworkCore.Internal.DbContextServices.get_Model()

Thrown on line 236 at https://github.com/aspnet/EntityFramework/blob/dev/src/EFCore.SqlServer/Storage/Internal/SqlServerTypeMapper.cs This is because the HasColumnType is set to nvarchar which is in the _disallowedMappings HashSet.

Steps to reproduce

Create a new database containing a single table. Ensure there is a nvarchar(4000) column. Scaffold the dbContext and create a controller/view and try and load the page. The following should be scaffolded:

entity.Property(e => e.Notes)
                    .HasColumnName("notes")
                    .HasColumnType("nvarchar");

The issue appears to be GetStoreType in EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs where it is not returning “nvarchar(max)” because the maxLength parameter isn’t set to -1.

Further technical details

EF Core version: 2 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: W10 IDE:Visual Studio 2017 Preview 2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:40 (18 by maintainers)

github_iconTop GitHub Comments

2reactions
mfields3commented, Jul 17, 2017

That isn’t an issue with SQL Server. Written in the scaffolding is some raw SQL which returns the data you’ve shared in the above table. There is a check to see if the size of the field is equal to maximum size of that type of field and if so, return NULL. Therefore perhaps that should be changed to return -1 if that’s what the code expects?

1reaction
ErikEJcommented, Oct 4, 2017

@ajcvickers Is this being reconsidered for 2.0.1 ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changing columns from Nvarchar(max) to Nvarchar(4000) ...
I am changing columns from NVARCHAR(MAX) down to NVARCHAR(4000) the column has no need to be MAX and 4000 characters should be sufficient ......
Read more >
Size of NVARCHAR(4000) is not enough for my local ...
I'm trying to create a local variable in a stored procedure that consists of a long sql query. This sql query will be...
Read more >
EF Core - Missing related entity in class despite foreign key ...
Using Dapper to get nvarchar(max) returns a string trimmed to 4000 characters. Can this behaviour be changed? How to retrieve dropped stored procedure,...
Read more >
Are the limits of varchar(8000) and nvarchar(4000) ...
Yes, as far as I am aware, that is the main reason one would use VARCHAR(8000) or NVARCHAR(4000) instead of any size bigger...
Read more >
dbquery ef core example
Want to add a scenario which worked before but broke when upgrading to EF Core 3. ... from the generated context (we use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found