1

I have an application which utilizes a webservice to get data on a regular basis, and needs to insert the records into a Sql Server database. It needs to check the table if the unique key exists or not, if it exists, the record has to be updated, it not, the record should be inserted.

I need a solution like Merge in oracle database to eliminate thinking about duplicate records, and something like bulk insert, in EF to insert all data at once.

I used AddRange and SaveChanges, but it uses several inserts and updates in DB, which takes long time to complete.

3
  • Bulk insert to a staging table, then run a MERGE on the database (or separate INSERT/UPDATE statements, MERGE doesn't really add much for simple scenarios). The staging table can be made an in-memory table if you're using SQL Server 2016+, for that sweet extra performance. Commented Jun 19, 2019 at 14:05
  • @JeroenMostert does it support EF? Commented Jun 19, 2019 at 14:11
  • I'd be disappointed if there was not some easy way to get EF to use the TDS bulk protocol for inserts (normally exposed through SqlBulkCopy), as it is the fastest way to get rows from client to server. But I don't know EF well enough. If there is no way to do it with EF it would still be worth doing without it. Commented Jun 19, 2019 at 14:14

1 Answer 1

1

There are free 3rd party NuGet packages that do bulk inserts for you, such as https://www.nuget.org/packages/RudeySH.EFUtilities. But this is not exactly what you asked for.

And then there's a popular commercial one at https://entityframework-extensions.net/. It adds support for bulk merge. https://entityframework-extensions.net/bulk-merge. Obviously, usage depends on your budget.

Sign up to request clarification or add additional context in comments.

Comments