[英]Entity Framework Core bulk update underlying collections
Is there a known extension for Entity Framework Core that can do this bulk update as in this SqlRaw example?是否有 Entity Framework Core 的已知扩展可以像此 SqlRaw 示例中那样执行此批量更新?
dbc.Database.ExecuteSqlRawAsync(
"UPDATE [Assembly] SET OnHold = @OnHold, UpdatedWith = @UpdatedWith, UpdatedAt = @UpdatedAt, UpdatedById = @UpdatedById FROM [Assembly] INNER JOIN Station ON [Assembly].StationID = Station.ID INNER JOIN Project ON Station.ProjectID = Project.ID WHERE Project.ID = @ProjectID",
onHold, updatedWith, updatedAt, updatedById, projectID)
I would suggest linq2db.EntityFrameworkCore<\/a> (disclaimer: I'm one of the creators)我会建议
linq2db.EntityFrameworkCore<\/a> (免责声明:我是创作者之一)
Then you can do the following:然后您可以执行以下操作:
var updateQuery =
from a in ctx.Assembly
join s in ctx.Station on a.SationId equals s.ID
join p in ctx.Project on s.ProjectId equals p.ID
where p.ID == projectId
select a;
var recordsAffected = await updateQuery
.Set(a => a.OnHold, onHold)
.Set(a => a.UpdatedWith, updatedWith)
.Set(a => a.UpdatedAt, a => Sql.CurrentTimeStamp)
.Set(a => a.UpdatedById, updatedById)
.UpdateAsync();
The IQueryable.ToQueryString
method introduced in Entity Framework Core 5.0 may help reduce/simplify the raw SQL used in your code. Entity Framework Core 5.0 中引入的
IQueryable.ToQueryString
方法可能有助于减少/简化代码中使用的原始 SQL。 This method will generate SQL that can be included in a raw SQL query to perform a bulk update of records identified by that query.此方法将生成可包含在原始 SQL 查询中的 SQL,以对该查询标识的记录执行批量更新。
For example:例如:
var query =
from a in dbc.Assembly
join s in dbc.Station on a.StaionId equals s.Id
join p in dbc.Project on s.ProjectId equals p.Id
where p.Id == projectId
select a.Id;
var sql = $@"
UPDATE Assembly
SET OnHold = {{0}}, UpdatedWith = {{1}}, UpdatedAt = {{2}}, UpdatedById = {{3}}
WHERE Id IN ({query.ToQueryString()})
";
dbc.Database.ExecuteSqlRawAsync(sql, onHold, updatedWith, updatedAt, updatedById);
The major drawback of this approach is the use of raw SQL.这种方法的主要缺点是使用原始 SQL。 However I don't know of any reasonable way to avoid that with current Entity Framework Core capabilities - you're stuck with this caveat, or the caveat of introducing a dependency on another library such as linq2db.EntityFrameworkCore as mentioned in another answer here.
但是,我不知道有任何合理的方法可以通过当前的 Entity Framework Core 功能来避免这种情况 - 你被这个警告所困扰,或者在另一个答案中提到的引入对另一个库(如linq2db.EntityFrameworkCore )的依赖的警告here。
If (when) the following issue is addressed in the future then we are likely to have a better answer to this question: Bulk (ie set-based) CUD operations (without loading data into memory) #795如果(何时)将来解决以下问题,那么我们可能会对这个问题有更好的答案: Bulk (ie set-based) CUD operations (without loading data into memory) #795
Just a little patience there will be the built-in BulkUpdate()
as well as BulkDelete
methods in EFCore which will be delivered in EFCore 7.0稍有耐心,EFCore 中将提供内置的
BulkUpdate()
和BulkDelete
方法,这些方法将在 EFCore 7.0 中提供
context.Customers.Where(...).BulkDelete();
context.Customers.Where(...).BulkUpdate(c => new Customer { Age = c.Age + 1 });
context.Customers.Where(...).BulkUpdate(c => new { Age = c.Age + 1 });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.