AnswerBun.com

C# Entity Framework: Update Only First Batch Records and Stop

Stack Overflow Asked by Artportraitdesign1 on August 29, 2020

Is there way with EF Core Bulk extensions to only update first few 10000 rows? write message, update next batch, write message in loop until complete?

I know there is Batch Size, however if there is only 5 million to update, and I want only update a certain amount, write a message, in continual loop until complete, how can this be done?

Should I use Top or Take?

I want to write "Hello", every few batches.

while {

await _dbContext.Set<Product>()
    .Where(x => x.Manufacturer == "ABC Company" &&
                x.StartYear == 2019 &&
                x.ProductType.ProductTypeDescription == "Electronics")
    .BatchUpdateAsync(x => new Product(){
                Manufacturer = "XYZ Company",
                StartYear = 2020 });

Console.WriteLine("hello"):

https://github.com/borisdj/EFCore.BulkExtensions

Using EF Core 3.1.

Company does not want to use SignalR

2 Answers

It looks like BatchSize is only used for bulk insert. For update the expression is translated to a single SQL UPDATE statement, which doesn't operate by "batches".

Correct answer by David Browne - Microsoft on August 29, 2020

Try this.

if loop until first few 10,000

int iEnd = 5;

for (var i = 0; i <= iEnd; i++)
{
    var products = await _dbContext.Set<Product>().Where(x => x.Manufacturer == "ABC Company" &&
                    x.StartYear == 2019 &&
                    x.ProductType.ProductTypeDescription == "Electronics").Skip(i * 10000).Take(10000).ToList();

    products.ForEach(x =>
    {
         x.Manufacturer = "XYZ Company";
         x.StartYear = 2020;
    });
    _dbContext.Set<Product>().UpdateRange(products); // or you can use BulkUpdate here.
}

_dbContext.SaveChanges();

If loop until 5 millions.

int i = 0;
Boolean IsContinue = true;
while (IsContinue)
{
     var products = await _dbContext.Set<Product>().Where(x => x.Manufacturer == "ABC Company" &&
                x.StartYear == 2019 &&
                x.ProductType.ProductTypeDescription == "Electronics").Skip(i * 10000).Take(10000).ToList();

     if (products.Count() == 0)
          IsContinue = false;
     else
     {
          products.ForEach(x =>
          {
                x.Manufacturer = "XYZ Company";
                x.StartYear = 2020;
          });
          _dbContext.Set<Product>().UpdateRange(products); // or you can use BulkUpdate here.
      }
      i++;
}

_dbContext.SaveChanges();

Answered by Asherguru on August 29, 2020

Add your own answers!

Related Questions

DASK can’t recognize workers on EMR

0  Asked on December 25, 2021 by cphoenix

         

Use external help file for C# programmed powershell cmdlet

0  Asked on December 25, 2021 by rivers-shall

     

Generate lexicographic permutations

2  Asked on December 23, 2021 by vishnu-prakash

     

How can i add a black layer over top of image with opacity?

3  Asked on December 23, 2021 by user13851719

   

How to add function to onChange in Input cmponent?

2  Asked on December 23, 2021 by will-black

       

Global variable to available

3  Asked on December 23, 2021 by havard-kleven

     

Integrating HTML pages to laravel

3  Asked on December 23, 2021 by user11195802

       

Run a counter in the background

1  Asked on December 23, 2021 by legacy-coding

   

Read from .csv, calculate, write on .csv

2  Asked on December 23, 2021 by irham-dollah

         

How to synchronize data between threads

2  Asked on December 23, 2021 by summit

     

Output is not as expected

3  Asked on December 23, 2021 by a4n

   

AWS Lambda query to Redshift once a day

2  Asked on December 23, 2021 by xbeta

         

Ask a Question

Get help from others!

© 2022 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP