Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# ef核心更新多个项目,然后保存_C#_Entity Framework_Asp.net Core_Entity Framework Core - Fatal编程技术网

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()
基本上我需要加快速度,所以我假设如果我更新items属性,然后为所有这些属性调用savechangesync(),它将对数据库进行一次调用,而不是在每个项的循环中进行调用

那么ctx.Items.Update(项目)呢?拥有它有意义吗?我想我需要这个,否则它怎么知道要更新什么,对吗

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 
}