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.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…