Developer bit

Bulk updates in Entity Framework

Use bulk updates to update entities with a single command (without the need of the change tracker).

Bulk updates in Entity Framework

Use bulk updates to update entities with a single command (without the need of the change tracker).

The entities will not be loaded into memory, which results in more efficient updates and deletes.

For more info, see my blog post "New in Entity Framework 7: Bulk Operations with ExecuteDelete and ExecuteUpdate", or the documentation.

// Use ExecuteDeleteAsync to delete entities
await context.Set<CartItem>()
.ExecuteDeleteAsync();
// Use Where as a filter to find specific entities (to delete or update)
await context.Set<CartItem>()
.Where(c => c.Sku == "ABC")
.ExecuteDeleteAsync();
// Use ExecuteUpdateAsync to update entities
await context.Set<CartItem>()
.ExecuteUpdateAsync(s =>
// Use SetProperty with a fixed value to update a property
s.SetProperty(p => p.Amount, 100)
);
await context.Set<CartItem>()
.Where(c => c.Amount == 2)
.ExecuteUpdateAsync(s =>
// Use SetProperty to update a property using its current value
s.SetProperty(p => p.Amount, v => v.Amount + 1)
);

Feel free to update this developer bit on GitHub, thanks in advance!

Enjoying the blog?

Support my work

If you enjoyed this post and found it useful, consider supporting my work. It helps me keep creating and sharing content like this. Thank you!