我正在尝试使用实体框架批量更新记录。我尝试过 Entity Framework.ExtensionsUpdate
method.
The Update
方法能够批量更新具有相同更新值集的一组记录。
Example:
Id - Quantity
Record 1 - A - 10
Record 2 - B - 20
Record 3 - C - 30
我们可以通过简单的调用来批量更新以上所有记录
Records.Update(new => Record { Quantity = 100 });
如何使用不同数量批量更新每条记录Entityframework.Extensions
或者以任何其他方法,哪个可以更快地完成批量更新?
如果您不想使用 SQL 语句,可以使用 Attach 方法来更新实体,而无需先加载它:
using (myDbEntities db = new myDbEntities())
{
try
{
//disable detection of changes to improve performance
db.Configuration.AutoDetectChangesEnabled = false;
//for all the entities to update...
MyObjectEntity entityToUpdate = new MyObjectEntity() {Id=123, Quantity=100};
db.MyObjectEntity.Attach(entityToUpdate);
//then perform the update
db.SaveChanges();
}
finally
{
//re-enable detection of changes
db.Configuration.AutoDetectChangesEnabled = true;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)