我正在尝试使用实体框架批量更新记录。我已尝试实体框架。扩展Update
方法。Update
方法能够批量更新具有相同更新值集的一组记录。
示例:
Id - Quantity
Record 1 - A - 10
Record 2 - B - 20
Record 3 - C - 30
我们可以通过简单的调用来批量更新以上所有记录
Records.Update(new => Record { Quantity = 100 });
如何使用Entityframework.Extensions
或任何其他方法批量更新具有不同数量的每条记录,这样可以更快地完成批量更新?
9条答案
按热度按时间3bygqnnd1#
如果不想使用SQL语句,可以使用Attach方法来更新实体,而不必先加载它:
jogvjijk2#
使用
ExecuteSqlCommand
:或者
ExecuteStoreCommand
:pbwdgjma3#
EFCore中有内置的
ExecuteUpdate
和ExecuteDelete
方法,这些方法是由EFCore 7.0提供的yvt65v4c4#
如果只想修改少数属性,请使用此方法:
lztngnrs5#
a)E核心.批量扩展-批处理更新异步
https://github.com/borisdj/EFCore.BulkExtensions
“实体框架核心扩展:批量操作(插入、更新、删除、读取、上插、同步)和批处理(删除、更新)。库是轻量级的,非常高效,具有所有最常用的CRUD操作。被Microsoft推荐的前20个EF核心扩展选中。”
B)或EF扩展-从查询更新
资源:
https://entityframework-extensions.net/update-from-query
https://stackoverflow.com/a/63460251/12425844
为什么UpdateFromQuery比保存更改、批量保存更改和批量更新快?
UpdateFromQuery直接在SQL中执行语句,例如UPDATE [表名] SET [设置列和值] WHERE [键]。
其他操作通常需要一次或多次数据库往返,这会降低性能。
ibrsph3r6#
I found an easy way to do that without any 3rd party packages:
By adding one generic extension method
SetValue
you can simply write:Example:
As you can see, any value matching the
Where
condition can be set explicitly to a new value, so hereBike
will be replaced byBicycle
. You can query the table afterwards to see the changes really persisted.Of course, you could also omit the
Where
statement, if you want to change all records like:Entity framework (EF) / LINQ tracks those changes and when you call .SubmitChanges() - as you can see in the SQL tab if you're using LinqPad - it will create SQL code as follows:
For small changes, this is ok, but for large tables it is becoming inefficient, because it uses the ID column to identify and change a record, and not the Description column as defined by .SetValue.
Theoretically EF could optimize this, but as you can see, it doesn't do it. So if you want true bulk operations you need to run a SQL command instead or create a stored procedure (for complex queries) which you're calling via EF.
Extension method
SetValue
This extension method does the trick (no other 3rd party packages required):
Note: The example above uses the Nutshell example database, which you can easily create by following this link and the code is written for LinqPad 6 but can be adapted easily (LinqPad 6 uses .NET Core, but you can try it with LinqPad 5 as well for the .NET Framework).
3bygqnnd7#
在EF 6中我们在每个表中都有AddRange方法。文档建议这个方法比使用许多add方法要快得多。因此,可以在一个临时表中插入所有可更新的记录,并使用一条sql语句批量更新主表。
编辑:这个Document建议AddRange只优化变更检测,它不改变变更应用到数据库的方式。
vfhzx4xs8#
批量更新可以通过简单的EF而不是单独的扩展方法分三步完成:-
这将在一个批次中发送多个更新查询。
h22fl7wq9#
可能通过使用
UpdateRange([NotNullAttribute] params TEntity[] entities)