-
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
SQL for string-updates sometimes uses nvarchar(450) if a string field is used in an index (not key) #8322
Comments
Note for triage: this looks like another manifestation of #6835 /cc @divega @AndriySvyryd |
@iJungleboy What is the actual column type for Value? |
@iJungleboy Also, does the index IX_EAV_Values2 containing the column actually exist in the database, or is it only defined in EF? |
@AndriySvyryd Try using nvarchar(max) for all TVV columns in SQL Server 2008 |
@ajcvickers The real type is nvarchar(max), and no, it was not in the DB index at all, that must be a minor bug in the DB-First model generation. @AndriySvyryd As noted the column is already nvarchar(max) and the SQL version is 2012 |
@iJungleboy Would it be possible to get a repro that caused this index to get reverse engineered? Hopefully, if you can post the table definitions we can do it from there. |
No problem - it's part of an open source EAV solution https://github.com/2sic/eav-server for the .net CMS DNN. Here's the table definition, I assume it's complete but tell me if I missed something.
|
@iJungleboy Thanks very much for posting that so quickly. Hopefuly quick follow-up question: do you know if any other table references the "Value" column in, for example, a foreign key constraint? |
I don't think so, but I've included the full SQL to create the tables and stored procedures which are in use. |
Entity Framework Core auto-generates SQL-Statements to cause updates to happen. When a string-value is used as a key, it will by default use nvarchar(450) instead of nvarchar(max).
The problem is that this also happens in a few (but not all) code generated for string values which are in an index (instead of a key). Specifically, the temporary table which is used in the SQL for merge operations will incorrectly assume an nvarchar(450) and then fail when trying to place the initial nvarchar(max) value in this temp table.
The exception will then look be:
SqlException: String or binary data would be truncated.
The full exception shown is:The SQL which was sent to the SQL Server was the following:
As you can see, the
@p5
is correctly treated as annvarchar(max)
but the temporary table@toInsert0
creates a column[Value]
which is cast asnvarchar(450)
- triggering this problem.In my case I had a DB first model which simply imported the indexes into the OnModelCreating ModelBuilder on my DbContext resulting in this index being managed:
When I commented out the information that
e.Value
is part of the index, everything worked flawlessly:It's important to note that many changes to this data type were not affected/hurt by the index-information. Just some very specific ones caused this problem.
Steps to reproduce
My code is too complex to use when reproducing, sorry. But I hope the full explanation above clarifies everything.
Further technical details
EF Core version: 1.1.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 Dev environment, Windows 2012 R2 Web Server, IIS
IDE: Visual Studio 2017
The text was updated successfully, but these errors were encountered: