字符串或二进制数据将被截断。语句已被终止 [英] String or binary data would be truncated.The statement has been terminated

查看:1177
本文介绍了字符串或二进制数据将被截断。语句已被终止的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用实体框架中的 update-database 命令更新我的数据库,并且使用消息引发错误:字符串或二进制数据将被截断。该声明已被终止



如何允许更新工作?

解决方案

取此代码第一个实体:

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

这将创建一个类型为$ code的列NVARCHAR MAX)这将让你几乎存储任何大小的文本。



现在我们应用这样的注释:

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

所以现在从这个迁移到缩短您的列只有50个字符。如果您的条目长度超过50个字符,您将收到该错误消息。



解决方案1 ​​



修复数据!删除长度超过50个字符的数据。使用SQL可以删除违规行:

  DELETE FROM MyTable 
WHERE LEN MyColumn)> 50

或者,更好的主意是手动截断数据:

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

解决方案2 (不是100%肯定这会奏效) p>

使用以下命令让迁移截断数据:

  Update-数据库-Force 


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?

解决方案

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

这篇关于字符串或二进制数据将被截断。语句已被终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆