Home > OS >  How to update a large amount of data with Entity Framework Core in C# with SQL Server
How to update a large amount of data with Entity Framework Core in C# with SQL Server

Time:12-29

I want to update a large amount of data (around 2 million records) with Entity Framework Core in C# (.NET 6), but I have performance issues.

I tried this:

var data = await _context.Set<MyObject>()
                         .Where(x => x.field != string.Empty)
                         .ToListAsync();  

data.ForEach(x => x.field = _myService.GetNewValue(x.field))

_context.Set<MyObject>().UpdateRange(data);
_context.SaveChanges();

How can I improve performance?

Thanks!

CodePudding user response:

  • Update to EF Core 7
  • Set a high Command Timeout value
  • Use the new ExecuteUpdate method

or just use a SQL statement or stored procedure, you can call it with the ExecuteSqlCommand method in EF Core

  • Related