How to Debug SQL Exception: String or Binary Data Would Be Truncated in .NET EF Core

While building a new application for an old database, I ran into an issue where I was too trying update a column with to long of a string. Entity Framework gives an error similar to String or binary data would be truncated. The statement has been terminated. This error message is rather useless if your database has a lot of columns.

If you're using Entity Framework, you can wrap your Save method in a try-catch to add more information.

try {
  await _dbContext.SaveChangesAsync();
}catch(Exception ex){
  foreach(var entry in _dbContext.ChangeTracker.Entries().Where(e => e.State != EntityState.Unchanged)){
    foreach(var prop in entry.CurrentValues.Properties){
      var val = prop.PropertyInfo.GetValue(entry.Entity);
      Console.WriteLine($"{prop.ToString()} ~ ({val?.ToString().Length})({val})");

You can view your terminal and spot the entity that's too long.

Matt Ferderer

Software engineer who spends his time learning about building teams, project management, software architecture, C#, .NET Core, Blazor, JavaScript, TypeScript, Azure, user experience, web security, and performance.