Update to 2K records <Clients> how to do a single update instead of 2k separately

Asked

Viewed 94 times

7

I have a Customer Model and need to do an update on EnviadoEmailCobranca para 0

I’m doing like this:

    var clientes = db.Clientes.Where(w => w.Status == 4);

    foreach (var item in clientes)
    {
        item.EnviadoEmailCobranca = false;

        db.Entry(item).State = EntityState.Modified;
        db.SaveChanges();
    }

But that’s kind of donkey, because I’m doing 2,000 separate updates. There is something like:

Update tabela_Clientes where EnviadoEmailCobranca=0 where Status =4

I didn’t mean to do it like this:

var SQL = "Update tabela_Clientes where EnviadoEmailCobranca=0 where Status =4";
dbMailEnable.Database.ExecuteSqlCommand(SQL);
  • I even took it out, but the performance was far from reasonable.. I did a test with 6,000 records and it took a lot longer than doing a direct Executesqlcommand...

1 answer

7


How to do a single update instead of 2k separately?

Not using native Entity Framework. Simple as that. It doesn’t meet this kind of demand you have.

Already the Entityframework.Extended meets, that’s right batch update implemented. His Nuget package is here.

Use:

db.Clientes
  .Where(w => w.Status == 4)
  .Update(w => new Cliente { EnviadoEmailCobranca = 0 });
  • 1

    show.... thanks, up to pq 2mil is an initial number can rise to 5mil or more..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.