EF异常:字符串或二进制数据将被截断。声明已经终止。 [英] EF Exception: String or binary data would be truncated. The statement has been terminated.?

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

问题描述

我读过很多与这个问题有关的帖子,但找不到答案。
我试图将大量数据从Excel加载到SQL Server。
成千上万的记录。我收到这个例外:


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


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






我的问题 - 我怎么可能知道什么记录和什么字段值导致这个?



EF异常没有具体的细节,除了我提到的。



任何帮助都不胜感激。



有些人要求代码片段,但实际上很简单,问题不在于代码: p>

  // employees是一个List< Employee>使用(var context = new Entities())从Excel 
加载的集合
{
employees.ForEach(e => context.Employee.AddObject(e));
context.SaveChanges();
}

另外建议使用 DbEntityValidationException 仅在Entity Framework 5.0中可用)不起作用, catch 块没有捕获异常。

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

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






$ b

现在我可以看到电子邮件太长了。 div>

  catch(DbEntityValidationException ex)
{
foreach(exeEntityValidationErrors中的var item)
{
// ..在这里检查
}
}

你可以找到你需要的信息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天全站免登陆