C# EF如何创建更新一个包含500万行的SQLite表中的100万行所需的SQL?它会逐行更新吗?

C# EF如何创建更新一个包含500万行的SQLite表中的100万行所需的SQL?它会逐行更新吗?,c#,.net,entity-framework,linq,entity-framework-core,C#,.net,Entity Framework,Linq,Entity Framework Core,当我想使用EntityFrameworkCore时,如果我进行更新。例如,更新此表中类型为“a”的每一行,使其名称为“a”,那么EF如何处理该名称 public class Deck { public string DeckGuid { get; set; } public string Name { get; set; } public string Type { get; set; } } EF是否检索或导致检索每一行,然后使用Type=“A

当我想使用EntityFrameworkCore时,如果我进行更新。例如,更新此表中类型为“a”的每一行,使其名称为“a”,那么EF如何处理该名称

public class Deck
{
    public string DeckGuid { get; set; } 
    public string Name     { get; set; }
    public string Type     { get; set; }   
}
EF是否检索或导致检索每一行,然后使用Type=“A”选择这些行,然后逐个更新这些行

还有没有不同的方法可以编写更新代码,这样它就不会一行一行地进行更新,例如,如果有一百万行需要更新

另外,如果只更新了一行,并且列类型上有一个索引,那么情况又如何呢。是否考虑了这些因素?

查看EF扩展。当通过EF处理大量数据时,它非常有用。 更新场景示例(来自他们的文档):

//删除
context.Items.Where(a=>a.ItemId>500).BatchDelete();
context.Items.Where(a=>a.ItemId>500).BatchDeleteAsync();
//更新(使用表达式参数)支持递增/递减
其中(a=>a.ItemId新项{Quantity=a.Quantity+100});
//可以是值“+100”或变量“+incrementStep”(int incrementStep=100;)
//更新(通过简单对象)
context.Items.Where(a=>a.ItemId a.ItemId a.ItemId查看EF扩展。当通过EF处理大量数据时,它非常有用。
更新场景示例(来自他们的文档):

//删除
context.Items.Where(a=>a.ItemId>500).BatchDelete();
context.Items.Where(a=>a.ItemId>500).BatchDeleteAsync();
//更新(使用表达式参数)支持递增/递减
其中(a=>a.ItemId新项{Quantity=a.Quantity+100});
//可以是值“+100”或变量“+incrementStep”(int incrementStep=100;)
//更新(通过简单对象)

context.Items.Where(a=>a.ItemId a.ItemId a.ItemId在需要更新大量行的情况下,最好通过
DbContext.Database.ExecuteSqlCommand()直接执行SQL语句
。您需要确保这是一个新的DbContext,因为DbContext跟踪的任何缓存实体都不会被更新,更改将直接转到数据库,因此任何缓存实体都需要在之后重新加载。如果要更新大量行,则最好执行SQL语句d直接通过
DbContext.Database.ExecuteSqlCommand()
。您需要确保这是一个新的DbContext,因为DbContext正在跟踪的任何缓存实体都不会被更新,更改将直接转到数据库,因此以后需要重新加载任何缓存实体。
    //Delete
    context.Items.Where(a => a.ItemId >  500).BatchDelete();
    context.Items.Where(a => a.ItemId >  500).BatchDeleteAsync();

    // Update (using Expression arg.) supports Increment/Decrement 
    context.Items.Where(a => a.ItemId <= 500).BatchUpdate(a => new Item { Quantity = a.Quantity + 100 });
      // can be as value '+100' or as variable '+incrementStep' (int incrementStep = 100;)
      
    // Update (via simple object)
    context.Items.Where(a => a.ItemId <= 500).BatchUpdate(new Item { Description = "Updated" });
    context.Items.Where(a => a.ItemId <= 500).BatchUpdateAsync(new Item { Description = "Updated" });
    // Update (via simple object) - requires additional Argument for setting to Property default value
    var updateColumns = new List<string> { nameof(Item.Quantity) }; // Update 'Quantity' to default value('0'-zero)
    var q = context.Items.Where(a => a.ItemId <= 500);
    int affected = q.BatchUpdate(new Item { Description = "Updated" }, updateColumns);//result assigned to variable