8

I am updating my database using the update-database command in Entity Framework and it is raising an error with the message: String or binary data would be truncated. The statement has been terminated

How can I allow the update to work?

3

5 Answers 5

18

Take this code first entity:

public class Something
{
    public string SomeProperty { get; set; }
}

That will create a column of type NVARCHAR(MAX) which will let you pretty much store any size text.

Now we apply an annotation like this:

public class Something
{
    [MaxLength(50)]
    public string SomeProperty { get; set; }
}

So now the migration from this has to shorten your column to 50 characters only. If you have entries that are longer that 50 characters, you will get that error message.

Solution 1

Fix the data! Remove any data longer than 50 characters. Using SQL, either delete the offending rows:

DELETE FROM MyTable
WHERE LEN(MyColumn) > 50

Or, probably a better idea is to manually truncate the data:

UPDATE MyTable
SET MyColumn = LEFT(MyColumn, 50)
WHERE LEN(MyColumn) > 50

Solution 2 (not 100% sure this will work)

Let the migration truncate the data by using this command:

Update-Database -Force
2
  • 7
    Using the -Force flag doesn't override the truncation error. However, running the UPDATE MyTable SET MyColumn = LEFT(MyColumn, 50) did allow the Update-Database command to complete successfully. Sep 29, 2015 at 15:45
  • VS2019. -Force doesn't work. I had a column NVARCHAR(60) and wanted to set it to NVARCHAR(6) with a code-first migration. There was just one existing row in the table with 10 characters in the column, while all the rest were 6, and "Update-Database" raised this exception, both with and without "-Force". I just used the SQL Server console to fix this one entry (reduced to 6 characters) and the Update-Database worked. Nov 12, 2021 at 20:29
3

Here is an article on how to debug this or any EF related issues:

String or binary data would be truncated. The statement has been terminated

Basically what you have to do is to turn on 'SQL Server Profiler' and turn on 'Exception' and 'RPC:Starting' profilers. When you get your error in profiler, you will be able to see full query that was executed by EF. From there you can copy and paste to MS SQL Server Management Studio run it manually and identify the issue.

3

In EF dot net core "code first" one can do this SQL Statement:

UPDATE MyTable
SET MyColumn = LEFT(MyColumn, 50)
WHERE LEN(MyColumn) > 50

In the Migration UP part, one can execute the sql like this:

modelBuilder.sql("UPDATE MyTable SET MyColumn = LEFT(MyColumn, 50) WHERE LEN(MyColumn) > 50");

Please note that the statement has to be beofre the before the alter of string length.

1

To me is sounds like you are trying to alter the length/size of a field in a table to be "smaller" then some of the data that already resides in it.

So for example if i have a varchar(28) type field field in my table-- with data that already inside that is 28 characters long... and then I try to execute an alter table to reduce the size to varchar(25) it will say: String or binary data would be truncated

It coul also arise IF you are trying to stuff a String that is lets say 30 cahraters long into a field that only supports 28 characters...

So it can happen if you are trying to insert data into a field and the data is to big to fit essientially

5
  • yes i am trying to alter the length from NVARCHAR(MAX) to nvarchar(60),NVARCHAR(MAX) to nvarchar(30),NVARCHAR(MAX) to nvarchar(5).when i passed the command in package manager console,add-migration DataAnnotations and update-database,this error arises,tell me the solution kindly Jul 24, 2014 at 10:23
  • execute this SQL to find the max length of data in column: SELECT name, LENGTH(name) AS mlen FROM mytable ORDER BY mlen DESC LIMIT 1
    – g00dnatur3
    Jul 24, 2014 at 10:30
  • use the max length value when altering your table... otherwise you need to write a special DML SQL UPDATE query to truncate the data explicitly
    – g00dnatur3
    Jul 24, 2014 at 10:32
  • @user1500191 Pretty sure LIMIT 1 isn't available in SQL Server, it's a MySQL thing right?
    – DavidG
    Jul 24, 2014 at 10:35
  • sql-server: select max(len(Desc)) from table_name
    – g00dnatur3
    Jul 24, 2014 at 10:51
0

It is a property length issue.You are trying to insert property's value which length is less than your declared property's length.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.