Skip to content

Commit c4054dd

Browse files
authored
Create ef-core-batch-update.md
1 parent 42219df commit c4054dd

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
---
2+
Permalink: ef-core-batch-update
3+
---
4+
5+
# Batch Update
6+
7+
> This feature is now available on [Entity Framework Classic - Update from Query](http://entityframework-classic.net/update-from-query). Entity Framework Classic is a supported version from the latest EF6 code base. It supports .NET Framework and .NET Core and overcomes some EF limitations by adding tons of must-haves built-in features.
8+
9+
## Introduction
10+
11+
Updating using Entity Framework Core can be very slow if you need to update hundreds or thousands of entities with the same expression. Entities are first loaded in the context before being updated which is very bad for the performance and then, they are updated one by one which makes the update operation even worse.
12+
13+
**EF+ Batch Update** updates multiple rows using an expression in a single database roundtrip and without loading entities in the context.
14+
15+
{% include template-example.html %}
16+
```csharp
17+
18+
// using Z.EntityFramework.Plus; // Don't forget to include this.
19+
20+
// UPDATE all users inactive for 2 years
21+
var date = DateTime.Now.AddYears(-2);
22+
ctx.Users.Where(x => x.LastLoginDate < date)
23+
.Update(x => new User() { IsSoftDeleted = 1 });
24+
25+
```
26+
[Try it](https://dotnetfiddle.net/0CBNK1)
27+
28+
## Limitations
29+
30+
- **DO NOT** support Complex Type
31+
- **DO NOT** support TPC
32+
- **DO NOT** support TPH
33+
- **DO NOT** support TPT
34+
35+
If you need to use one of this feature, you need to use the library [Entity Framework Extensions](https://entityframework-extensions.net/)
36+
37+
### EF Core & Client Evaluation
38+
39+
_Do not use this feature if Client Evaluation is enabled_
40+
41+
`Batch Update` use the SQL generated by EF Core. When a filter is made on client-side, it means the filtering happens in the application and not in the SQL executed.
42+
43+
In other words, even if you put a filter, all rows tables could be potentially updated if the filter is made on the client-side.
44+
45+
We always recommend to [disable the client evaluation](https://docs.microsoft.com/en-us/ef/core/querying/client-eval#optional-behavior-throw-an-exception-for-client-evaluation) to avoid performance issue in your application.
46+
47+
48+
## Requirements
49+
50+
- **EF+ Batch Delete:** Full version or Standalone version
51+
- **Entity Framework Version:** EFCore
52+
- **Minimum Framework Version:** .NET Framework 4
53+
54+
## Conclusion
55+
56+
**EF+ Batch Update** is the most efficient way to update records using an expression. You drastically improve your application performance by removing the need to retrieve and load entities in your context and by performing a single database roundtrip instead of making one for every record.
57+
58+
Need help getting started? [[email protected]](mailto:[email protected])
59+
60+
We welcome all comments, ideas and suggestions to improve our library.

0 commit comments

Comments
 (0)