通过条件列表删除/更新 EF Core 7 中实体的有效方法

问题描述 投票:0回答:1

我需要删除/更新表中给定数量的行。

在其中我是这样解决的:

  await context.MyItems
    .Where(x => itemsIds.Contains(x.Id))
    .ExecuteUpdateAsync(f => f.SetProperty(x => x.State, state));

这允许我更新给定 ID 列表中的项目(1,2,5,7, ...)

但我不知道如何在表中执行相同的操作,我需要的不是按 id 列表,而是按多个属性列表进行筛选:

var itemsToUpdate = new List<Identifier>(){ ....}

标识符是

public class Identifier
{
    DateTime date {get;set;}
    string code {get;set;}
    string owner {get;set;}
    
}

知道如何在 EF Core 中高效地执行此操作吗?因为我认为我没有得到其他解决方案,而是一个存储过程:-(

c# .net entity-framework
1个回答
0
投票

您可以使用 BulkExtensions 来实现:https://github.com/borisdj/EFCore.BulkExtensions

例如:

 await context.MyItems
.Where(x => itemsIds.Contains(x.Id))
.BatchUpdateAsync(f => new Item { State = state });
© www.soinside.com 2019 - 2024. All rights reserved.