efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Scaffolding broken for nvarchar(4000)

Open mfields3 opened this issue 9 years ago • 40 comments

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

mfields3 avatar Jul 17 '17 16:07 mfields3

nvarchar(4000) is different from nvarchar(max) (difference of 1). SQL Server shows weird behavior for max_length for that specific type (or similar types almost at the max).

Type max_length value
nvarchar(200) 400
nvarchar(4000) NULL
nvarchar(max) -1

We should report this to SQL Server team. Meanwhile we can handle this case specifically depending on timeline of former.

smitpatel avatar Jul 17 '17 17:07 smitpatel

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?

mfields3 avatar Jul 17 '17 19:07 mfields3

Look at: https://github.com/aspnet/EntityFramework/blob/dev/src/EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs#L365

ErikEJ avatar Jul 18 '17 05:07 ErikEJ

^^ Exactly. Should that therefore be returning -1 instead of NULL?

mfields3 avatar Jul 18 '17 06:07 mfields3

@mfields3 It should actually return 8000 for nvarchar(4000) !

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

ErikEJ avatar Jul 18 '17 07:07 ErikEJ

@ErikEJ It does return 8000, yet whoever wrote this CASE statement as part of scaffolding decided that when the length is equal to the maximum length of the field to return NULL.

Yet I believe as far as EF Core is concerned, the length of the string should be 4000, not 8000 nor MAX. nvarchar(max) allows 2 GB. Yet the field is defined as nvarchar(4000) which uses 8000 bytes, yet has a max length of 4000.

Perhaps instead of sys.columns we use CHARACTER_MAXIMUM_LENGTH in INFORMATION_SCHEMA.COLUMNS (assuming support for SQL 2008 onwards is ok).

mfields3 avatar Jul 18 '17 10:07 mfields3

The case statement is incorrect. It should return 8000. There seem to be no usefuleness of nulling out when length is maximum. When its type(max) SqlServer will return -1 anyway. I will make the change when I will get time to fix the issue.

smitpatel avatar Jul 18 '17 16:07 smitpatel

Scaffolding is also broken for varchar(8000) for I'm guessing is the same problem as above.

ghost avatar Aug 15 '17 20:08 ghost

If you are stuck (as I was) you can use SQLite / SQL Server Compact Toolbox to scaffold your tables.

ADefWebserver avatar Aug 26 '17 02:08 ADefWebserver

I have the same problem with fields with different lengths nvarchar(100), nvarchar(50) and the error is in runtime. When will be the new version that fixes this issue?

JuanIrigoyen avatar Aug 30 '17 00:08 JuanIrigoyen

@JuanIrigoyen The runtime isn't going to change. If you want to specify the store type abstractly, including the size, unicode, etc., then do so using the appropriate APIs/attributes (e.g. MaxLength, IsUnicode) and don't specify any explicit store type. If you want to specify an explicit store type, then do it fully, including the size.

ajcvickers avatar Aug 30 '17 17:08 ajcvickers

@Gaelmart That code looks right to me. Is it doing something unexpected?

ajcvickers avatar Aug 31 '17 00:08 ajcvickers

I think it is unexpected that the autogenerated code fails like that, so yes!

When i create my models like this: Scaffold-DbContext "Server=(local);Database=MyDB;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -OutputDir Models

this is what i get in one of the fields:

entity.Property(e => e.Text) .IsRequired() .HasColumnType("nvarchar");

and when running the code I get this error: 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.

So there must be an error in the scaffolding. This code and database worked before 2.0

AxelDalenAndersenP avatar Sep 22 '17 17:09 AxelDalenAndersenP

Azure-hosted .Net Core 2.0 web app using Razor Pages:

I'm getting this same error when trying to build a Controller by scaffolding in EF Core 2.0. Oddly, the model that I'm referencing in the wizard was generated from a database as per this tutorial in MS Docs. The difference being that I'm using AdventureWorksLT, an example DB made available in Azure.

I'm wondering if it's the User Defined Data Types causing the issue. Specifically Name:nvarchar(50).

Output

Finding the generator 'controller'...
Running the generator 'controller'...
Attempting to compile the application in memory.
Attempting to figure out the EntityFramework metadata for the model and DbContext: 'Customer'
info: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[0]
      User profile is available. Using 'C:\Users\Ryan Varley\AppData\Local\ASP.NET\DataProtection-Keys' as key repository and Windows DPAPI to encrypt keys at rest.
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. StackTrace:
   at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerTypeMapper.ValidateTypeName(String storeType)
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.   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()
   at Microsoft.EntityFrameworkCore.Infrastructure.EntityFrameworkServicesBuilder.<>c.<TryAddCoreServices>b__7_1(IServiceProvider p)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitFactory(FactoryCallSite factoryCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(IServiceCallSite callSite, TArgument argument)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass22_0.<RealizeService>b__0(ServiceProvider provider)
   at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
   at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
   at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
   at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
   at Microsoft.EntityFrameworkCore.DbContext.Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.get_Instance()
   at Microsoft.EntityFrameworkCore.Infrastructure.AccessorExtensions.GetService[TService](IInfrastructure`1 accessor)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func`1 factory)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.VisualStudio.Web.CodeGeneration.EntityFrameworkCore.EntityFrameworkServices.TryCreateContextUsingAppCode(Type dbContextType, Type startupType)
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.<BuildCommandLine>b__6_0()
   at Microsoft.Extensions.CommandLineUtils.CommandLineApplication.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.ActionInvoker.Execute(String[] args)
   at Microsoft.VisualStudio.Web.CodeGeneration.CodeGenCommand.Execute(String[] args)

TableSQL

CREATE TABLE [SalesLT].[Product](
	[ProductID] [int] IDENTITY(1,1) NOT NULL,
	[Name] [dbo].[Name] NOT NULL,
	[ProductNumber] [nvarchar](25) NOT NULL,
	[Color] [nvarchar](15) NULL,
	[StandardCost] [money] NOT NULL,
	[ListPrice] [money] NOT NULL,
	[Size] [nvarchar](5) NULL,
	[Weight] [decimal](8, 2) NULL,
	[ProductCategoryID] [int] NULL,
	[ProductModelID] [int] NULL,
	[SellStartDate] [datetime] NOT NULL,
	[SellEndDate] [datetime] NULL,
	[DiscontinuedDate] [datetime] NULL,
	[ThumbNailPhoto] [varbinary](max) NULL,
	[ThumbnailPhotoFileName] [nvarchar](50) NULL,
	[rowguid] [uniqueidentifier] NOT NULL,
	[ModifiedDate] [datetime] NOT NULL,
 CONSTRAINT [PK_Product_ProductID] PRIMARY KEY CLUSTERED 
(
	[ProductID] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF),
 CONSTRAINT [AK_Product_Name] UNIQUE NONCLUSTERED 
(
	[Name] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF),
 CONSTRAINT [AK_Product_ProductNumber] UNIQUE NONCLUSTERED 
(
	[ProductNumber] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF),
 CONSTRAINT [AK_Product_rowguid] UNIQUE NONCLUSTERED 
(
	[rowguid] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF)
)
GO

RCVarley avatar Sep 24 '17 20:09 RCVarley

I am getting the same error but for varchar(MAX) and varchar(4000) and VARCHAR(8000).

jvelezc avatar Sep 27 '17 00:09 jvelezc

Exactly what i experienced, there is for sure a bug in the scaffolding tool, or the EF Core

AxelDalenAndersenP avatar Sep 27 '17 04:09 AxelDalenAndersenP

I am also experiencing the same issue after updating application from 1.0 to core 2.0.

SwapnilChandan avatar Sep 27 '17 06:09 SwapnilChandan

Marking for re-triage

ajcvickers avatar Sep 29 '17 16:09 ajcvickers

Sorry for the slow response. It turns out that my particular issue was a result of (hangs head in shame) a stray nvarchar value in my DbContext class, which has already been reported and addressed. Fortunately I can confirm that the Alias didn't screw anything up.

RCVarley avatar Sep 29 '17 17:09 RCVarley

@ajcvickers Is this being reconsidered for 2.0.1 ?

ErikEJ avatar Oct 04 '17 05:10 ErikEJ