0

I am using a lot of bulk insert calls similar to this:

List<Object> list = someSetOfData;
dbcontext.BulkInsert(list);

Each object has multiple string properties that can only be a certain length in the database. Based on the amount of data that is getting inserted, it would slow down the application if I manually check that each string is the correct length before I insert it. Is there a way that I can tell BulkInsert to automatically truncate any strings that are too long (so that it won't throw exceptions)? Also if there is a way to do that, will it tell me when it actually truncates a string?

And if not, what else would you recommend trying?

Jeremy P
  • 1,309
  • 2
  • 13
  • 22
  • It seems like you should enforce things like property lengths in your model - not in the method you use to insert the data. You can decorate model properties with StringLength attributes so the model matches the database. – snow_FFFFFF Jun 08 '21 at 20:05

1 Answers1

0

This seemed interesting, you would have to call something else before hitting savechanges.

Entity Framework 4.0 Automatically Truncate/Trim String Before Insert

Another solution for .NET Core: https://medium.com/@paullorica/automatically-truncate-strings-in-an-ef-core-entity-based-on-maxlength-property-93e17ea7cd43