简体   繁体   English

如何使用Entity Framework Extensions库执行批量的“ INSERT IGNORE”

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

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 ? 有没有一种方法可以立即运行所有查询,而不必只是在遇到第一个错误时就停止运行,或者直接应用IGNORE

You should check the InsertIfNotExists options. 您应该检查InsertIfNotExists选项。 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. 我在其中一个字段的表中有一个UNIQUE键。 How do I set it for bulk operations? 如何为批量操作设置它?

You can customize the key with the ColumnPrimaryKeyExpression option. 您可以使用ColumnPrimaryKeyExpression选项自定义键。

ctx.BulkInsert(list, options =>
{
    options.ColumnPrimaryKeyExpression = x => new { x.ColumnKey1, x.ColumnKey2 };
    options.InsertIfNotExists = true;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM