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

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

问题描述

我已阅读与此问题相关的许多职位,但找不到答案。
我试图从Excel加载大量数据到SQL Server。
成千上万的记录。而我得到这个异​​常:




字符串或二进制数据将被截断。该声明已经
终止。




显然,有些值超过数据库中的字段大小。
中的错误来自SQL Server的AFIK






我的问题 - 我怎么可能知道是什么记录哪些字段值造成的?



有在EF例外没有具体的细节,除了我提到的。



任何帮助表示赞赏。



有的问的代码片段,但它实际上是非常简单的,这个问题是不是与代码:

  //员工是一个List<员工>通过收集从Excel 
装(VAR背景=新的实体())
{
employees.ForEach(E => context.Employee.AddObject(e)条);
context.SaveChanges();
}



另外,建议的方法来使用 DbEntityValidationException (其中仅在实体框架5.0)不工作,在块没赶上例外。

 <$ C可用$ C>试
{
IMPORTDATA();
}
赶上(DbEntityValidationException前)
{
的foreach(在ex.EntityValidationErrors VAR项)
{
// ...
}
}

,我发现到目前为止,唯一的解决办法是使用SQL Server Profiler,并定义以下事件监测:



在这里输入的形象描述





现在我可以看到邮件是太长了。


解决方案

 赶上(DbEntityValidationException前)
{
的foreach(在ex.EntityValidationErrors VAR项)
{
// .. 。检查这里
}
}

您可以找到您所需要的内部信息foreach循环。



希望有所帮助。


I have read many posts related to this issue, but couldn't find an answer. I am trying to load a large amount of data from Excel into SQL Server. Thousands of records. And I am getting this exception:

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

Obviously some values exceed the field size in the database. The error comes from SQL Server AFIK.


My question - How could I possibly know what record and what field value caused this?

There are no specific details in EF exception, except the one I mentioned.

Any help is appreciated.

Some asked for the code fragment, but it's actually very simple, the problem is not with the code:

// employees is a List<Employee> collection loaded from Excel
using (var context = new Entities())
{
    employees.ForEach(e => context.Employee.AddObject(e));
    context.SaveChanges();
}

Also the suggested approach to use DbEntityValidationException (which is only available in Entity Framework 5.0) is not working, the catch block didn't catch the exception.

try
{
    ImportData();
}
catch (DbEntityValidationException ex)
{
    foreach (var item in ex.EntityValidationErrors)
    {
        //...
    }
}

The only solution that I found so far is to use SQL Server Profiler, and define the following events to monitor:

Now I can see the Email is too long.

解决方案

catch (DbEntityValidationException ex)
{
    foreach (var item in ex.EntityValidationErrors)
    {
        //... inspect here 
    }
}

You can find the information you need inside foreach loop.

Hope that helps.

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

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