efcore.bulkextensions如何在条件上更新记录?

发布于 2025-02-03 02:23:54 字数 615 浏览 4 评论 0 原文

我正在使用 efcore.bulkextensions 在表中插入和更新记录。但是我的状态更新记录有问题。

例如,我有15个记录(10个记录,插入5个记录,5个更新)。我需要插入10,但仅更新2个,因为3个记录在更新的属性中具有旧值(数据库包含更多最新数据)。

如果我使用 efcore.bulkextensions 这样:

_dbContext.BulkInsertOrUpdateAsync(entitiesList, _bulkConfig);

将插入10个记录,并将更新5个记录。因此,数据库中的数据将由较旧的数据进行更新。

为了解决我的问题,我想要这样的问题:

_dbContext.BulkInsertOrUpdateAsync(entitiesList, _bulkConfig,
    (oldRecord, newRecord) => newRecord.UpdatedAt > oldRecord.UpdatedAt);

您能否提出一些有效的方法来解决此问题,以 efcore.bulkextensions

I am using EFCore.BulkExtensions for insert and update records in a table. But I have a problem with update records on condition.

For example, I have 15 records (10 to insert, 5 to update). I need to insert 10, but update only 2, because 3 records have old value in UpdatedAt property (database contains more recent data).

If I use EFCore.BulkExtensions like this:

_dbContext.BulkInsertOrUpdateAsync(entitiesList, _bulkConfig);

10 records will be inserted and 5 records will be updated. So the data in the database will be updated by older ones.

To solve my problem I want something like this:

_dbContext.BulkInsertOrUpdateAsync(entitiesList, _bulkConfig,
    (oldRecord, newRecord) => newRecord.UpdatedAt > oldRecord.UpdatedAt);

Can you suggest some efficient way to solve this problem with EFCore.BulkExtensions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ゃ人海孤独症 2025-02-10 02:23:54

这不是 efcore.bulkextensions 的直接答案,而是使用。请注意,我是创作者之一。

await context.SomeTable
    .ToLinqToDBTable()
    .Merge()
    .Using(entitiesList)
    .On((t, s) => t.Id == s.Id)
    .InsertWhenNotMatched()
    .UpdateWhenMatchedAnd((t, s) => s.UpdatedAt > t.UpdatedAt)
    .MergeAsync();

为EF Core 2.x选择适当的软件包2.x,EF Core 3.1.x的3.x,等。

This is not direct answer for EFCore.BulkExtensions, but alternative how to do that with linq2db.EntityFrameworkCore. Note that I'm one of the creators.

await context.SomeTable
    .ToLinqToDBTable()
    .Merge()
    .Using(entitiesList)
    .On((t, s) => t.Id == s.Id)
    .InsertWhenNotMatched()
    .UpdateWhenMatchedAnd((t, s) => s.UpdatedAt > t.UpdatedAt)
    .MergeAsync();

Select appropriate package 2.x for EF Core 2.x, 3.x for EF Core 3.1.x, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文