Scaffolding broken for nvarchar(4000)
See original GitHub issueWhen 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:
- Created 6 years ago
- Comments:40 (18 by maintainers)
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?
@ajcvickers Is this being reconsidered for 2.0.1 ?