Linq2db: BulkCopyOptions object cannot be reused across multiple tables

Created on 14 Jan 2019  路  2Comments  路  Source: linq2db/linq2db

If you use the same BulkCopyOptions object on two different tables, it silently only inserts the rows into the first table it is used on.

```c#
using (var conn = new DataConnection(/.../))
{
var table1 = conn.CreateTable("Table1");
var table2 = conn.CreateTable("Table2");

    var bulkCopyOptions = new BulkCopyOptions {MaxBatchSize = 1000};

    table1.BulkCopy(bulkCopyOptions,
            new[] { new TestClass {TestColumn = 1 } });
    table2.BulkCopy(bulkCopyOptions, 
            new[] { new TestClass {TestColumn = 2 } });
    Console.WriteLine(string.Join(",", table1.Select(x => x.TestColumn))); // "1,2"
    Console.WriteLine(string.Join(",", table2.Select(x => x.TestColumn))); // ""
}

```

The error is caused by the if statements in the following code:

https://github.com/linq2db/linq2db/blob/8163ccb411a4cb5df610b58bba34562522b2faf9/Source/LinqToDB/Data/DataConnectionExtensions.cs#L1439-L1441

It's not immediately obvious why these fields need to be on the options object at all, but if they must, then some sort of exception should be thrown if any of the schema information is attempting to be changed on re-use.

Environment details

linq2db version: 2.6.2
Database Server: SQL Server 2016
Operating system: Windows 10
Framework version: .NET Framework 4.6.2

has-pr bug

Most helpful comment

I have no chance to check for now, but looks like we have PR for this: https://github.com/linq2db/linq2db/pull/1520

All 2 comments

I have no chance to check for now, but looks like we have PR for this: https://github.com/linq2db/linq2db/pull/1520

Duplicate of #1520

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sdanyliv picture sdanyliv  路  3Comments

MaceWindu picture MaceWindu  路  4Comments

a-karpov-parc picture a-karpov-parc  路  4Comments

MajidSafari picture MajidSafari  路  4Comments

Scratch-net picture Scratch-net  路  6Comments