Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when inserting the same row more than once #112

Closed
timmetcalfe opened this issue Dec 13, 2018 · 3 comments
Closed

Error when inserting the same row more than once #112

timmetcalfe opened this issue Dec 13, 2018 · 3 comments
Labels

Comments

@timmetcalfe
Copy link

Hi,
Sometimes duplicate rows may exist in the data I'm inserting or updating. When there are duplicate rows, the following error is generated:

'SqlException: The MERGE statement attempted to UPDATE or DELETE the same row more than once. This happens when a target row matches more than one source row. A MERGE statement cannot UPDATE/DELETE the same row of the target table multiple times. Refine the ON clause to ensure a target row matches at most one source row, or use the GROUP BY clause to group the source rows.'

I could check the data and remove the duplicate rows but would rather it was handled automatically. Is there a way for BulkExtensions to just insert or update one of the duplicate rows?

Thanks
Tim

@borisdj
Copy link
Owner

borisdj commented Dec 13, 2018

That's precisely what InsertOrUpdate method does.
As said in ReadMe:

BulkInsertOrUpdate method can be used when there is need for both operations but in one connection to database.
It makes Update when PK(PrimaryKey) is matched, otherwise does Insert.

Additionaly there is UpdateByProperties for specifying custom properties, by which we want update to be done.

@timmetcalfe
Copy link
Author

timmetcalfe commented Dec 13, 2018 via email

@borisdj
Copy link
Owner

borisdj commented Dec 14, 2018

Consistency of data rests on input, so I can only agree with the way you already suggested which is to make a method that will first filter out duplicates, since library currently does not handle this.
If you have this situation more then once then you could override Bulk method and add there removal of doubles before calling base method.
Here is how to make Bulk override, in this example it was to Set AuditInfo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants