Automatically truncate strings in an EF Core Entity based on MaxLength property

Paul Lorica
2 min readJun 19, 2019

--

Have you ever gotten that pesky “string or binary data would be truncated” exception when trying to save your entities in SQL?

As you may already know, this just means that you are trying insert data(commonly a string) which is more than the size of the column in SQL.

There are different and better ways to solve this which include

  1. simply increasing the size of the column, or
  2. adding validations before it evens reaches the code that saves to SQL.

But in my case, I was simply told to truncate the strings. The data that will be truncated is not important. This is not often the case

The entity that I needed to truncate the strings from contained 50+ string properties. Manually finding the length and truncating each property would be tedious.

In the DBContext model builder, you will find that string properties are decorated with a HasMaxLength PropertyBuilder.

I’ve developed an extension method that will cycle through all string properties of an entity and truncate them by their MaxLength. The following code is below.

Here is how it is used.

The sample code can be found here:

https://github.com/paul-lorica/efcore-truncatestrings-HasMaxLength

--

--