-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Scaffolding broken for nvarchar(4000) #9188
Comments
We should report this to SQL Server team. Meanwhile we can handle this case specifically depending on timeline of former. |
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? |
^^ Exactly. Should that therefore be returning -1 instead of NULL? |
@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 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. Perhaps instead of sys.columns we use CHARACTER_MAXIMUM_LENGTH in INFORMATION_SCHEMA.COLUMNS (assuming support for SQL 2008 onwards is ok). |
The case statement is incorrect. It should return 8000. There seem to be no usefuleness of nulling out when length is maximum. When its |
Scaffolding is also broken for varchar(8000) for I'm guessing is the same problem as above. |
If you are stuck (as I was) you can use SQLite / SQL Server Compact Toolbox to scaffold your tables. |
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 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. |
@Gaelmart That code looks right to me. Is it doing something unexpected? |
I think it is unexpected that the autogenerated code fails like that, so yes! When i create my models like this: this is what i get in one of the fields: entity.Property(e => e.Text) and when running the code I get this error: So there must be an error in the scaffolding. |
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 Output
TableSQL
|
I am getting the same error but for varchar(MAX) and varchar(4000) and VARCHAR(8000). |
Exactly what i experienced, there is for sure a bug in the scaffolding tool, or the EF Core |
I am also experiencing the same issue after updating application from 1.0 to core 2.0. |
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. |
@ajcvickers Is this being reconsidered for 2.0.1 ? |
Is there maybe a workaround for this? |
@MrBirdman I will consider a fix for EF Core Power Tools - does a replace of
=>
work for you? |
What about when we input more than 4000 characters? |
@ErikEJ yeah it might work for a while, i've used your tool to generate the context but it came out the same so i dropped it :) |
@MrBirdman Fixed in the latest daily release, pls try it out and let me know if it solves your problem, then I will publish the fix on VS Marketplace - https://github.com/ErikEJ/SqlCeToolbox/wiki/EF-Core-Power-Tools |
@ErikEJ I'm getting a "The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." message when i try to download SQLite / SQL Server Compact Toolbox v4.7.384 |
@MrBirdman This link works for me: http://vsixgallery.com/extensions/f4c4712c-ceae-4803-8e52-0e2049d5de9f/extension.vsix |
Actually i'm not sure this has done anything: |
Do not see anything relating to nvarchar on that screenshot. What is the runtime error you get? |
The left side is generated with EF, while the right side is with the toolbox. This table has several varchar(max) and nvarchar(max) columns, and it doesn't add or change anything to the model. |
@MrBirdman Do not use the Toolbox, use EF Core Power Tools (as per my wiki and direct link above)! Again, what runtime error do you get? (The Toolbox uses version 1.1 scaffolding) |
@ErikEJ We're going to discuss it, but no promises yet. Even if the team decides that it meets the bar, it will still need to go through patch approval before we will know for sure. |
Triage: we're going to try to get this fix into 2.0.1. See #9963. |
I am getting 'varchar is not supported' when connecting to sql 2008 r2. I have tried all the suggestions above; any ideas. |
@gate21 replace "varchar" with "varchar(4000)" in the generated code... Or use EF Core Power Tools |
I am using Code first. In my case I get the error on any varchar size. |
Have you tried : ?
|
yes I have tried HasColumnType("varchar(xxxx)"). I am using EFCore.sqlserver 2.0.1 |
@gate21 Please file a new issue including a runnable project/solution or complete code listing that demonstrates the behavior you are seeing so that we can investigate. |
Guys thanks for your help. While trying to debug the issue I was having, I left the RegisterConfigurationFromAssembly code in place "this was picking up other configuration that I have not fixed". Once I saw that removed it and registered the congifurationObject I was debugging "varchar(xxx)" did fix the issue." Regards |
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.
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:
The issue appears to be
GetStoreType
in EFCore.SqlServer/Scaffolding/Internal/SqlServerDatabaseModelFactory.cs where it is not returning "nvarchar(max)" because themaxLength
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
The text was updated successfully, but these errors were encountered: