zzzprojects / Dapper-Plus

Dapper Plus - High-Efficient Bulk Actions (Insert, Update, Delete, and Merge) for .NET
https://dapper-plus.net/
383 stars 85 forks source link

Error omitted "String or binary data would be truncated" #81

Closed ElenaShlykova closed 3 years ago

ElenaShlykova commented 3 years ago

Hello, We found an issue with bulk insert when trying to insert data that did not match the column size in the database. For example, we have an nvarchar (3) column in db and we are trying to insert text containing 5 characters. When we use Bulk Insert, no errors occur and the value in the database is truncated to 3 chars without any warning.

With pure SQL there will be an error: image

We would like to receive an error when we use dapper.plus. Is it possible?

JonathanMagnan commented 3 years ago

Hello @ElenaShlykova ,

By default, we keep the current behavior depending on what is used (SqlBulkCopy or a SqlCommand). They act differently but we keep this the logic for backward compatibility.

However, it is possible to force it.

By operation

connection.UseBulkOptions(options => options.AutoTruncate = false).BulkInsert(list);

By specific mapping

DapperPlusManager.Entity<EntitySimple>().Table("EntitySimples").UseBulkOptions(options => options.AutoTruncate = false);

Globally

BulkOperationManager.BulkOperationBuilder = builder =>
{
    builder.AutoTruncate = false;
};

Let me know if that answer correctly to your request

Best Regards,

Jon

ElenaShlykova commented 3 years ago

Thanks! it works. Is there any documentation where I can find an explanation about such settings?

JonathanMagnan commented 3 years ago

I don't think @ElenaShlykova ,

We will eventually re-write the documentation, but unfortunately, for now, a lot of hidden features will stay... hidden ;(

Let just hope that we find enough time soon for it!