c# – Update in 2000 records<Clientes> how to do a single update instead of 2k separately

Question:

I have a Model of clients and I need to update EnviadoEmailCobranca para 0

I'm doing it 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 this is kind of dumb , because I'm doing 2,000 separate updates. There is something like:

Update tabela_Clientes where EnviadoEmailCobranca=0 where Status =4

I didn't want to do it like this:

var SQL = "Update tabela_Clientes where EnviadoEmailCobranca=0 where Status =4";
dbMailEnable.Database.ExecuteSqlCommand(SQL);

Answer:

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

Not using native Entity Framework. That simple. It doesn't meet this kind of demand that you have.

EntityFramework.Extended, on the other hand, meets , as it has batch update implemented. His NuGet package is here .

Use:

db.Clientes
  .Where(w => w.Status == 4)
  .Update(w => new Cliente { EnviadoEmailCobranca = 0 });
Scroll to Top