|
| 1 | +# EF Core Hot-Path Queries & Set-Based Operations |
| 2 | + |
| 3 | +A focused EF Core companion demonstrating a lean read path, a stable compiled |
| 4 | +query, a named soft-delete query filter, and server-side set-based updates and |
| 5 | +deletes without external database infrastructure. |
| 6 | + |
| 7 | +## Full tutorial |
| 8 | + |
| 9 | +[EF Core Advanced Modeling & Performance: Owned Types, Converters, JSON/Temporal Tables, Compiled Queries](https://www.dotnet-guide.com/tutorials/ef-core/advanced-modeling-performance/) |
| 10 | + |
| 11 | +## Version note |
| 12 | + |
| 13 | +The full tutorial was written around .NET 8 / EF Core 8. |
| 14 | + |
| 15 | +This companion targets: |
| 16 | + |
| 17 | +```text |
| 18 | +.NET 10 |
| 19 | +EF Core 10 |
| 20 | +Microsoft.EntityFrameworkCore.Sqlite 10.0.11 |
| 21 | +``` |
| 22 | + |
| 23 | +because that is the current DOTNET GUIDE repository baseline. |
| 24 | + |
| 25 | +## Focus |
| 26 | + |
| 27 | +```text |
| 28 | +SQLite in-memory |
| 29 | + -> named soft-delete filter |
| 30 | + -> DTO projection |
| 31 | + -> AsNoTracking |
| 32 | + -> EF.CompileAsyncQuery |
| 33 | + -> ExecuteUpdateAsync |
| 34 | + -> ExecuteDeleteAsync |
| 35 | + -> fresh-context verification |
| 36 | +``` |
| 37 | + |
| 38 | +This sample is intentionally not a benchmark. |
| 39 | + |
| 40 | +## Why SQLite? |
| 41 | + |
| 42 | +SQLite gives the companion a real relational database without: |
| 43 | + |
| 44 | +- credentials; |
| 45 | +- a server; |
| 46 | +- Docker; |
| 47 | +- Testcontainers; |
| 48 | +- generated database files. |
| 49 | + |
| 50 | +The low-level in-memory connection remains open for the lifetime of each |
| 51 | +scenario so multiple DbContext instances see the same database. |
| 52 | + |
| 53 | +SQLite is still a different provider from SQL Server and PostgreSQL. |
| 54 | + |
| 55 | +This sample does not validate provider-specific JSON, temporal-table, |
| 56 | +concurrency-token, migration, query-plan, or batching behavior. |
| 57 | + |
| 58 | +## Soft-delete filter |
| 59 | + |
| 60 | +The companion targets EF Core 10 and uses a named query filter: |
| 61 | + |
| 62 | +```csharp |
| 63 | +entity.HasQueryFilter( |
| 64 | + "SoftDeleteFilter", |
| 65 | + product => !product.IsDeleted); |
| 66 | +``` |
| 67 | + |
| 68 | +The purge path selectively disables it: |
| 69 | + |
| 70 | +```csharp |
| 71 | +IgnoreQueryFilters( |
| 72 | + ["SoftDeleteFilter"]) |
| 73 | +``` |
| 74 | + |
| 75 | +The EF Core 8 tutorial must continue to explain that older versions use one |
| 76 | +combined filter expression when several filters apply to the same entity. |
| 77 | + |
| 78 | +## Projection and tracking |
| 79 | + |
| 80 | +The read path projects only: |
| 81 | + |
| 82 | +```text |
| 83 | +Sku |
| 84 | +Name |
| 85 | +PriceCents |
| 86 | +StockQuantity |
| 87 | +``` |
| 88 | + |
| 89 | +into `ProductSummary`. |
| 90 | + |
| 91 | +It also uses `AsNoTracking` to make the read-only intent explicit. |
| 92 | + |
| 93 | +The tests verify that no entity entries remain in the DbContext change tracker. |
| 94 | + |
| 95 | +## Compiled query |
| 96 | + |
| 97 | +The sample uses one: |
| 98 | + |
| 99 | +```text |
| 100 | +EF.CompileAsyncQuery |
| 101 | +``` |
| 102 | + |
| 103 | +for a stable SKU lookup. |
| 104 | + |
| 105 | +This does not mean every query should be manually compiled. |
| 106 | + |
| 107 | +EF Core already caches ordinary queries by expression-tree shape. Explicit |
| 108 | +compiled queries bypass the normal cache lookup and are intended for measured |
| 109 | +hot paths with stable query shapes and simple scalar parameters. |
| 110 | + |
| 111 | +The sample makes no fixed latency-savings claim. |
| 112 | + |
| 113 | +## Set-based update |
| 114 | + |
| 115 | +```text |
| 116 | +ExecuteUpdateAsync |
| 117 | +``` |
| 118 | + |
| 119 | +increments stock for all visible products below the threshold in one |
| 120 | +set-based command. |
| 121 | + |
| 122 | +The application does not load those products before updating them. |
| 123 | + |
| 124 | +## Change-tracker caveat |
| 125 | + |
| 126 | +`ExecuteUpdateAsync` does not synchronize entity instances that were already |
| 127 | +tracked by the DbContext. |
| 128 | + |
| 129 | +The test suite deliberately proves this behavior. |
| 130 | + |
| 131 | +Use a clear context boundary, reload, or clear tracking when mixing set-based |
| 132 | +operations with tracked entities. |
| 133 | + |
| 134 | +## Set-based delete |
| 135 | + |
| 136 | +The soft-deleted row is hidden by default. |
| 137 | + |
| 138 | +The purge operation explicitly disables the named soft-delete filter and calls: |
| 139 | + |
| 140 | +```text |
| 141 | +ExecuteDeleteAsync |
| 142 | +``` |
| 143 | + |
| 144 | +The affected-row count is asserted. |
| 145 | + |
| 146 | +## Deterministic output |
| 147 | + |
| 148 | +```text |
| 149 | +EF Core Hot-Path Query Lab |
| 150 | +Visible products: 4 |
| 151 | +Compiled lookup: SKU-003 | Dock | 12900 cents |
| 152 | +Restocked products: 3 |
| 153 | +Purged soft-deleted products: 1 |
| 154 | +Final rows: 4 |
| 155 | +``` |
| 156 | + |
| 157 | +## Restore, build, and test |
| 158 | + |
| 159 | +```powershell |
| 160 | +dotnet restore ` |
| 161 | + .\EfCoreHotPathMinimal.slnx |
| 162 | +
|
| 163 | +dotnet build ` |
| 164 | + .\EfCoreHotPathMinimal.slnx ` |
| 165 | + --configuration Release ` |
| 166 | + --no-restore |
| 167 | +
|
| 168 | +dotnet test ` |
| 169 | + .\EfCoreHotPathMinimal.slnx ` |
| 170 | + --configuration Release ` |
| 171 | + --no-build |
| 172 | +``` |
| 173 | + |
| 174 | +## Run |
| 175 | + |
| 176 | +```powershell |
| 177 | +dotnet run ` |
| 178 | + --project .\src\EfCoreHotPathMinimal\EfCoreHotPathMinimal.csproj ` |
| 179 | + --configuration Release ` |
| 180 | + --no-build |
| 181 | +``` |
| 182 | + |
| 183 | +## Deliberately omitted |
| 184 | + |
| 185 | +- SQL Server; |
| 186 | +- PostgreSQL; |
| 187 | +- temporal tables; |
| 188 | +- JSON columns; |
| 189 | +- owned/complex types; |
| 190 | +- value converters; |
| 191 | +- encryption; |
| 192 | +- concurrency tokens; |
| 193 | +- migrations; |
| 194 | +- Testcontainers; |
| 195 | +- split queries; |
| 196 | +- logging/interceptors; |
| 197 | +- benchmarking. |
| 198 | + |
| 199 | +The complete tutorial covers the broader modeling and provider-specific |
| 200 | +concepts. |
| 201 | + |
| 202 | +## Verification |
| 203 | + |
| 204 | +- Target framework: .NET 10 |
| 205 | +- EF Core provider: SQLite 10.0.11 |
| 206 | +- Direct application packages: 1 |
| 207 | +- Tests: 8 |
| 208 | +- External services: none |
| 209 | +- Generated database files: none |
| 210 | +- Benchmarks: none |
| 211 | +- Last reviewed: 2026-08-14 |
0 commit comments