Skip to content

Commit 6f6fe36

Browse files
Merge pull request #521 from waqasm78/master
update docs
2 parents de9f648 + c569e0a commit 6f6fe36

23 files changed

+833
-0
lines changed

docs2/pages/ef-core-docs/documentations/batch-update/ef-core-batch-update.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ ctx.Users.Where(x => x.LastLoginDate < date)
2525
```
2626
[Try it](https://dotnetfiddle.net/0CBNK1)
2727

28+
## Scenarios
29+
30+
- [Query Criteria](scenarios/ef-core-batch-update-query-criteria.md)
31+
- [Executing Interceptor](scenarios/ef-core-batch-update-executing-interceptor.md)
32+
2833
## Limitations
2934

3035
- **DO NOT** support Complex Type
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
Permalink: ef-core-batch-update-executing-interceptor
3+
---
4+
5+
# Executing Interceptor
6+
7+
### Problem
8+
9+
You need to log the DbCommand information or change the CommandText, Connection or Transaction before the batch is executed.
10+
11+
### Solution
12+
13+
The **Executing** property intercepts the DbCommand with an action before being executed.
14+
15+
{% include template-example.html %}
16+
```csharp
17+
18+
// using Z.EntityFramework.Plus; // Don't forget to include this.
19+
20+
string commandText
21+
var date = DateTime.Now.AddYears(-2);
22+
ctx.Users.Where(x => x.LastLoginDate < date)
23+
.Update(x => new User() { IsSoftDeleted = 1 },
24+
x => { x.Executing = command => commandText = command.CommandText; });
25+
26+
```
27+
[Try it](https://dotnetfiddle.net/oHJl9R)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
Permalink: ef-core-batch-update-query-criteria
3+
---
4+
5+
# Batch Update using Query Criteria
6+
7+
## Problem
8+
9+
You need to update one or millions of records based on a query criteria and an expression.
10+
11+
## Solution
12+
13+
The **Update** IQueryable extension method updates rows matching the query criteria with an expression 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/kJRdnp)
27+
28+
## Batch UpdateAsync
29+
30+
### Problem
31+
32+
You need to update one or millions of records based on a query criteria and an expression asynchronously.
33+
34+
### Solution
35+
36+
The **UpdateAsync** IQueryable extension method updates asynchronously rows matching the query criteria with an expression without loading entities in the context.
37+
38+
{% include template-example.html %}
39+
```csharp
40+
41+
// using Z.EntityFramework.Plus; // Don't forget to include this.
42+
43+
// UPDATE all users inactive for 2 years
44+
var date = DateTime.Now.AddYears(-2);
45+
ctx.Users.Where(x => x.LastLoginDate < date)
46+
.UpdateAsync(x => new User() { IsSoftDeleted = 1 });
47+
48+
```
49+
[Try it](https://dotnetfiddle.net/TBfvz6)
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
Permalink: ef-core-linq-dynamic
3+
---
4+
5+
# LINQ Dynamic
6+
7+
> This feature is now available on [Entity Framework Classic - LINQ Dynamic](http://entityframework-classic.net/linq-dynamic). 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+
**LINQ Dynamic** in Entity Framework is supported through the [Eval-Expression.NET](http://eval-expression.net/) Library.
12+
13+
### Predicate
14+
15+
All LINQ predicate methods are supported. A string expression which return a Boolean function can be used as parameter.
16+
17+
- Deferred
18+
- SkipWhile
19+
- TakeWhile
20+
- Where
21+
- Immediate
22+
- All
23+
- Any
24+
- Count
25+
- First
26+
- FirstOrDefault
27+
- Last
28+
- LastOrDefault
29+
- LongCount
30+
- Single
31+
- SingleOrDefault
32+
33+
{% include template-example.html %}
34+
```csharp
35+
36+
var list = ctx.Where(x => "x > 2").ToList();
37+
var list = ctx.Where(x => "x > y", new { y = 2 }).ToList();
38+
39+
```
40+
[Try it in EF6](https://dotnetfiddle.net/Otm0Aa) | [Try it in EF Core](https://dotnetfiddle.net/1sTQwA)
41+
42+
## Options
43+
44+
- [Order && Select](options/ef-core-linq-dynamic-order-and-select.md)
45+
- [Execute](options/ef-core-linq-dynamic-execute.md)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
Permalink: ef-core-linq-dynamic-execute
3+
---
4+
5+
# Execute
6+
7+
The Execute method is the LINQ Dynamic ultimate methods which let you evaluate and execute a dynamic expression and return the result.
8+
9+
- Execute
10+
- Execute< TResult >
11+
12+
{% include template-example.html %}
13+
```csharp
14+
15+
var list = ctx.Execute<IEnumerable<int>>("Where(x => x > 2)");
16+
var list3 = ctx.Execute("Where(x => x > y).OrderBy(x => x).ToList()", new { y = 2 });
17+
18+
```
19+
[Try it](https://dotnetfiddle.net/LbU2at)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
Permalink: ef-core-linq-dynamic-order-and-select
3+
---
4+
5+
# Order && Select
6+
7+
All LINQ selector and order are supported. Most of them require the "Dynamic" suffix to not override default behavior (Ordering or selecting by a string is valid).
8+
9+
- OrderByDescendingDynamic
10+
- OrderByDynamic
11+
- SelectDynamic
12+
- SelectMany
13+
- ThenByDescendingDynamic
14+
- ThenByDynamic
15+
16+
{% include template-example.html %}
17+
```csharp
18+
19+
var list = context.Customers.OrderByDescendingDynamic(x => "x.Name").ToList();
20+
var list = context.Customers.SelectDynamic(x => "x.Name").ToList();
21+
22+
```
23+
[Try it](https://dotnetfiddle.net/8n2Xc0)
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
Permalink: ef-core-query-cache
3+
---
4+
5+
# Query Cache
6+
7+
> This feature is now available on [Entity Framework Classic - Query Cache](http://entityframework-classic.net/query-cache). 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+
Caching entities or query results to improve an application's performance is a very frequent scenario. Major ORM like NHibernate had this feature for a long time but, unfortunately for Entity Framework Core users, second level caching is only available through third party libraries.
12+
13+
Caching is very simple to understand, the first time a query is invoked, data are retrieved from the database and stored in the memory before being returned. All future calls will retrieve data from the memory to avoid making additional database round trips which drastically increases an application's performance.
14+
15+
**EF+ Query Cache** opens up all caching features for Entity Framework Core users.
16+
17+
To use caching, simply append to the query "FromCache" method before using an immediate resolution method like "ToList()" or "FirstOrDefault()".
18+
19+
{% include template-example.html %}
20+
```csharp
21+
22+
// using Z.EntityFramework.Plus; // Don't forget to include this.
23+
var ctx = new EntitiesContext();
24+
25+
// The first call perform a database round trip
26+
var countries1 = ctx.Countries.FromCache().ToList();
27+
28+
// Subsequent calls will take the value from the memory instead
29+
var countries2 = ctx.Countries.FromCache().ToList();
30+
31+
```
32+
[Try it](https://dotnetfiddle.net/EZZkhP)
33+
34+
## Scenarios
35+
36+
- [Query Criteria](scenarios/ef-core-query-cache-using-query-criteria.md)
37+
- [Query Deferred](scenarios/ef-core-query-cache-query-deferred.md)
38+
- [Tag & ExpireTag](scenarios/ef-core-query-cache-tag.md)
39+
- [Expiration](scenarios/ef-core-query-cache-expiration.md)
40+
- [Query Cache Control](scenarios/ef-core-query-cache-control.md)
41+
42+
## Real Life Scenarios
43+
44+
- Caching read-only data like application configuration.
45+
- Caching data that cQuery Cache Controlan only be modified via an importation like states & countries and make all cache entries related expired once the importation is completed (Tag Expiration).
46+
- Caching data that are frequently accessed but rarely modified like comments and expire the tag when a new comment is added or after one hour without any access (Tag Expiration & Sliding Expiration).
47+
- Caching statistics that don't require to be live like client count and expire them every hour (Absolute Expiration).
48+
49+
## Behind the code
50+
51+
When **FromCache** is invoked, The QueryCacheManager returns the result if a cache entry exists for the cache key. If no cache entry exists, the query is materialized then cached in the memory using the cache key before being returned. If a cache tag is specified, the cache key is also stored in a concurrent dictionary for all tags.
52+
53+
- **The Cache:** The memory cache is used by default.
54+
55+
- **The Cache Key:** The cache key is created by combining a cache prefix, all cache tags and the query expression.
56+
57+
- **The Query Materialized:** The query is materialized by either using "ToList()" method or "Execute()" method for query deferred.
58+
59+
## Limitations
60+
61+
- Entity Framework Core:
62+
- **DO NOT** support Cache Query Deferred (May be available when EF Core will be more stable)
63+
64+
## Requirements
65+
66+
- **EF+ Query Cache:** Full version or Standalone version
67+
- **Database Provider:** All supported
68+
- **Entity Framework Version:** EF Core
69+
- **Minimum Framework Version:** .NET Framework 4
70+
71+
## Conclusion
72+
73+
As we have seen, EF+ Query Cache follows a good architecture principle:
74+
75+
- **Flexible:** Tag & Cache Options make it possible to use Query Cache in a various number of scenarios.
76+
- **Extensible:** If there is something missing, the library creates your own extension method or your own cache to overcome the problem.
77+
- **Maintainable:** The easy to use API, documentation and available source code allows new developers to quickly understand this feature.
78+
- **Scalable:** Caching gets only better as the number of user/traffic grows by drastically reducing database round trip.
79+
80+
Need help getting started? [[email protected]](mailto:[email protected])
81+
82+
We welcome all comments, ideas and suggestions to improve our library.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
---
2+
Permalink: ef-core-query-cache-control
3+
---
4+
5+
# EF Query Cache Control
6+
7+
EF+ Cache is very flexible and lets you have full control over the cache.
8+
9+
You can use your own cache:
10+
11+
{% include template-example.html %}
12+
```csharp
13+
QueryCacheManager.Cache = new MemoryCache(new MemoryCacheOptions());
14+
context.Customers.FromCache().ToList();
15+
16+
```
17+
[Try it](https://dotnetfiddle.net/6ISVBT)
18+
19+
You can set default policy
20+
21+
{% include template-example.html %}
22+
```csharp
23+
24+
var options = new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromHours(2)};
25+
QueryCacheManager.DefaultMemoryCacheEntryOptions = options;
26+
context.Customers.FromCache().ToList();
27+
28+
```
29+
[Try it](https://dotnetfiddle.net/k1TOWX)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
---
2+
Permalink: ef-core-query-cache-expiration
3+
---
4+
5+
# EF+ Query Cache Expiration
6+
7+
All common caching features like absolute expiration, sliding expiration, removed callback are supported.
8+
9+
{% include template-example.html %}
10+
```csharp
11+
12+
// using Z.EntityFramework.Plus; // Don't forget to include this.
13+
var ctx = new EntitiesContext();
14+
15+
// Make the query expire after 2 hours of inactivity
16+
17+
var options = new MemoryCacheEntryOptions() { SlidingExpiration = TimeSpan.FromHours(2)};
18+
19+
var states = ctx.States.FromCache(options);
20+
21+
```
22+
[Try it](https://dotnetfiddle.net/aKPnTD)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
Permalink: ef-core-query-cache-query-deferred
3+
---
4+
5+
## EF+ Query Cache Query Deferred
6+
7+
Immediate resolution methods like **Count()** and **FirstOrDefault()** cannot be cached since their expected behavior is to cache the count result or only the first item.
8+
9+
{% include template-example.html %}
10+
```csharp
11+
12+
// Oops! The query is already executed, we cannot cache it.
13+
var count = ctx.Customers.Count();
14+
15+
// Oops! All customers are cached instead of customer count.
16+
var count = ctx.Customers.FromCache().Count();
17+
```
18+
[Try it](https://dotnetfiddle.net/3EXZBt)
19+
20+
**EF+ Query Deferred** has been created to resolve this issue, the resolution is now deferred instead of being immediate which lets us cache the expected result.
21+
22+
{% include template-example.html %}
23+
```csharp
24+
// using Z.EntityFramework.Plus; // Don't forget to include this.
25+
var ctx = new EntitiesContext();
26+
27+
// The count is deferred, it is now cached.
28+
var count = ctx.Customers.DeferredCount().FromCache();
29+
30+
```
31+
[Try it](https://dotnetfiddle.net/V0G6pe)
32+
33+
Query Deferred supports all Queryable extension methods and overloads.

0 commit comments

Comments
 (0)