EF Exception: String or binary data would be truncated. The statement has been terminated.?

c# entity-framework exception sql-server

Question

I read a lot of postings on this problem but was unable to come up with a solution. I'm attempting to import a sizable volume of Excel data into SQL Server. 1000s of records. And this exception is what I'm getting:

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

Of course, some numbers are larger than the database's field limits. AFIK, SQL Server is where the error originates.


How am I supposed to identify which record and which field value triggered this, is my query.

Except for the one I stated, there are no specific specifics in the EF exception.

We appreciate any assistance.

Some requested a code snippet, but the solution is actually quite straightforward and the code is not the issue:

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

The catch block failed to catch the exception, therefore the advised method of using DbEntityValidationException (which is only included in Entity Framework 5.0) does not work either.

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

Use SQL Server Profiler and set the following events to be monitored, according to The only remedy I could discover thus far:

enter image description here

enter image description here

I now realize the email is very long.

1
44
11/14/2012 3:41:52 PM

Popular Answer

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

The information you require is contained within the foreach loop.

Hope that was useful.

6
11/14/2012 2:58:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow