zzzprojects / Bulk-Operations

C# SQL Bulk Operations | High-performance C# bulk insert, update, delete and merge for SQL Server, SQL Azure, SQL Compact, MySQL, and SQLite.
https://bulk-operations.net
142 stars 36 forks source link

InsertIfNotExists does not work as expected #47

Closed asktuan closed 5 years ago

asktuan commented 5 years ago

User table has 2 columns: UserId INT, PRIMARY KEY, LoginName VARCHAR(255) sourceDt is DataTable and have exact same 2 columns above. Insert the second time I hit exception "Duplicate entry '108' for key 'PRIMARY'"

            using (var connection = DbAdapter.GetDbConnection(targetProviderName, targetConnString))
            {
                if(connection.State!= ConnectionState.Open)
                    connection.Open();
                // Bulk insert
                using (var bulk = new BulkOperation(connection))
                {
                    bulk.DestinationTableName = "User";
                    bulk.InsertIfNotExists = true;
                    bulk.ColumnMappings.Add("UserId", true);
                    bulk.ColumnMappings.Add("LoginName");
                    bulk.BulkInsert(sourceDt);
                }
            }
JonathanMagnan commented 5 years ago

Hello @asktuan ,

We tried it on our side but everything seems to work.

Here is an online example: https://dotnetfiddle.net/Mc67z6

Which database provider are you using? Perhaps you are using something else than SQL Server.

Best Regards,

Jonathan

asktuan commented 5 years ago

I'm using MySQL, in fact its MariaDB

JonathanMagnan commented 5 years ago

Thank,

We will try it for this one, perhaps the options is not implemented yet for this provider

asktuan commented 5 years ago

Would be perfect if MySQL is implemented. We're looking forward to it

JonathanMagnan commented 5 years ago

Hello @asktuan ,

The v2.15.9 has been released.

Could you try this version and let us know if the issue is now fixed on your side?

asktuan commented 5 years ago

Works great. InsertIfNotExists now works on MySQL (as well as MariaDB Thank you Jonathan