C# ef核心更新多个项目,然后保存
我需要更新数据库中的多个项目 我的问题是:C# ef核心更新多个项目,然后保存,c#,entity-framework,asp.net-core,entity-framework-core,C#,Entity Framework,Asp.net Core,Entity Framework Core,我需要更新数据库中的多个项目 我的问题是: 我是否需要使用ctx.Items.Update(item) 我是否可以先更新循环中的所有项,然后使用ctx.saveChangesSync() 基本上我需要加快速度,所以我假设如果我更新items属性,然后为所有这些属性调用savechangesync(),它将对数据库进行一次调用,而不是在每个项的循环中进行调用 那么ctx.Items.Update(项目)呢?拥有它有意义吗?我想我需要这个,否则它怎么知道要更新什么,对吗 using (var c
- 我是否需要使用ctx.Items.Update(item)李>
- 我是否可以先更新循环中的所有项,然后使用ctx.saveChangesSync()
using (var ctx = new MyCOntext())
{
var allToBeUpdated = await ctx.Items.Where(t=>t.ToBeUpdated == true).ToListAsync();
foreach (var item in allToBeUpdated)
{
item.ToBeUpdated = false;
ctx.Items.Update(item); // do I need this ?
}
await ctx.SaveChangesAsync(); // can I have the save changes async out of foreach? So I can update all the items at once rather than one by one
}
以下内容应足以保存这些值
using (var ctx = new MyCOntext())
{
var allToBeUpdated = await ctx.Items.Where(t=>t.ToBeUpdated == true).ToListAsync();
foreach (var item in allToBeUpdated)
{
item.ToBeUpdated = false; //change tracking will take care of tracking what is changed
}
await ctx.SaveChangesAsync(); // Save changes outside will update all the pending changes
}
以下内容应足以保存这些值
using (var ctx = new MyCOntext())
{
var allToBeUpdated = await ctx.Items.Where(t=>t.ToBeUpdated == true).ToListAsync();
foreach (var item in allToBeUpdated)
{
item.ToBeUpdated = false; //change tracking will take care of tracking what is changed
}
await ctx.SaveChangesAsync(); // Save changes outside will update all the pending changes
}