BulkInsertOrUpdate - Update if UniqueID existed else Insert new record

Micheale See 1 Reputation point
2022-02-24T09:10:10.803+00:00

Hi All,

Good day.

I have this code:-

using (TransactionScope trans = new TransactionScope())
{
using (SqlConnection conn = new SqlConnection(_Productrepository.Database.GetDbConnection().ConnectionString))
{
bulk.Setup<Product>(x => x.ForCollection(pd))
.WithTable("Products")
.WithBulkCopyCommandTimeout(0) // Default is 600 seconds
.WithBulkCopyBatchSize(50000) // Default is 0 - single batch
.WithBulkCopyEnableStreaming(true)
.WithSqlBulkCopyOptions(SqlBulkCopyOptions.TableLock)
.AddAllColumns()
.CustomColumnMapping(x => x.UniqueId, "UniqueID")
.BulkInsertOrUpdate()
.SetIdentityColumn(x => x.RequisitionID)
.MatchTargetOn(x => x.UniqueId);
bulk.CommitTransaction(conn);
}

                trans.Complete();
            }

However, it still showing duplicate record with the same UniqueID existed more than once. I want top update existing record if the UniqueID already existed in the existing table.

Can someone help me on this?

Thanks.

Regards,
Micheale

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,919 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Micheale See 1 Reputation point
    2022-03-03T05:46:33.573+00:00

    Hi,

    Sorry for late replied. I just back from Covid +ve Sick Leave.

    Thanks. Finally I figure out the issue not from my code. I have tested the function using http://.....azurewebsites.net/swagger User Interface. I can't inserted twice the same unique id. It'll get updated the record if the unique id already existed.

    Thanks.

    Regards,
    Micheale

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.