How do I execute a bulk `INSERT IGNORE` with Entity Framework Extensions library

.net c# entity-framework entity-framework-6 insert

Question

I'm trying to insert a large set of objects into the table but I don't have any efficient way to check if some records aren't already there. Every time I use this:

using Z.EntityFramework.Extensions.Core;
...
await ac.BulkInsertAsync(query, (o) => { o.?? });

it just stops the insert each time it finds a duplicate. Is there a way to either run all queries at once without it just stopping at the first error, or outright applying IGNORE?

1
1
6/12/2017 12:27:03 PM

Accepted Answer

You should check the InsertIfNotExists options. Only records that don't already exists will be inserted.

using Z.EntityFramework.Extensions.Core;
...
await ac.BulkInsertAsync(query, (o) => { o.InsertIfNotExists = true });

ANSWER Sub-Question

I have an UNIQUE key in my table on one of the fields. How do I set it for bulk operations?

You can customize the key with the ColumnPrimaryKeyExpression option.

ctx.BulkInsert(list, options =>
{
    options.ColumnPrimaryKeyExpression = x => new { x.ColumnKey1, x.ColumnKey2 };
    options.InsertIfNotExists = true;
});
3
6/12/2017 7:18:32 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow