MethodCache.EntityFrameworkCore
1.0.0-rc.12
See the version list below for details.
dotnet add package MethodCache.EntityFrameworkCore --version 1.0.0-rc.12
NuGet\Install-Package MethodCache.EntityFrameworkCore -Version 1.0.0-rc.12
<PackageReference Include="MethodCache.EntityFrameworkCore" Version="1.0.0-rc.12" />
<PackageVersion Include="MethodCache.EntityFrameworkCore" Version="1.0.0-rc.12" />
<PackageReference Include="MethodCache.EntityFrameworkCore" />
paket add MethodCache.EntityFrameworkCore --version 1.0.0-rc.12
#r "nuget: MethodCache.EntityFrameworkCore, 1.0.0-rc.12"
#:package MethodCache.EntityFrameworkCore@1.0.0-rc.12
#addin nuget:?package=MethodCache.EntityFrameworkCore&version=1.0.0-rc.12&prerelease
#tool nuget:?package=MethodCache.EntityFrameworkCore&version=1.0.0-rc.12&prerelease
MethodCache.EntityFrameworkCore
Invalidates MethodCache tags when Entity Framework Core commits a change. Map an entity type to the tags of the cached methods that read it, and every save that adds, changes or deletes one of its rows invalidates them, after the data is committed, never before.
Install it at the same version as MethodCache.
Why this package
- No invalidation code in your writes. An endpoint that loads an entity, changes it and calls
SaveChangesAsyncinvalidates the right tags by itself. - Never too early. Inside a transaction the tags wait for the commit, so no reader caches the old rows in between,
and a rollback drops them.
TransactionScopeworks too. - Old and new values. Moving a row to another category invalidates both categories, also when you update or delete the row without loading it first.
- The same tags as your methods.
"product:{Id}"on the entity matches"product:{id}"on a[Cache]method, formatted the same way, and a name that is not a property stops the host at startup. - Rows the database deletes for you.
Entity<User>(user => user.WhenDeleted("posts"))invalidates tags only when a user is deleted, for rows thatON DELETE CASCADEremoves with it, while a user saved on every sign-in clears nothing. - A warning where it can't help.
ExecuteUpdateandExecuteDeleteon a mapped type and raw SQL go past the change tracker; analyzer MC0027 reports them when the method doesn't invalidate. Mark your own invalidation helpers[InvalidatesCache]and a call to them counts. MC0028 reports a tag on a cached method that nothing in the project invalidates.
Usage
builder.Services.AddDbContext<ShopDb>(o => o.UseSqlite(connectionString));
builder.Services.AddScoped<IProductReads, ProductReads>();
builder.Services.AddMethodCache(mc => mc.UseEntityFrameworkCore<ShopDb>(ef => ef
.Entity<Product>("product:{Id}", "category:{CategoryId}")
.Entity<OrderLine>("top-sellers")));
public static async Task ChangePriceAsync(int id, decimal price, ShopDb db, CancellationToken ct)
{
var product = await db.Products.SingleAsync(p => p.Id == id, ct);
product.Price = price;
// Invalidates product:{id} and category:{CategoryId} once the save succeeded
await db.SaveChangesAsync(ct);
}
For a write the change tracker doesn't see, queue the tags yourself:
public static async Task RecalculateAsync(ShopDb db, IMethodCacheKeys keys, int categoryId, CancellationToken ct)
{
await using var transaction = await db.Database.BeginTransactionAsync(ct);
await db.Products
.Where(p => p.CategoryId == categoryId)
.ExecuteUpdateAsync(s => s.SetProperty(p => p.Price, p => p.Price * 1.1m), ct);
// ExecuteUpdateAsync goes past the change tracker: name the tags it changes
db.InvalidateAfterCommit(keys.Tag<IProductReads>(r => r.ListAsync(categoryId), "category:{categoryId}"));
await transaction.CommitAsync(ct);
}
Works with SQL Server, PostgreSQL, SQLite and the other relational providers, and on SaveChanges also with Cosmos DB and
MongoDB. The full guide, with when exactly each tag is invalidated, is
Entity Framework Core.
With row-level security, cached reads need a context of their own: see
Row-level security.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- MethodCache (= 1.0.0-rc.12)
- Microsoft.EntityFrameworkCore.Relational (>= 10.0.0)
- Microsoft.Extensions.Hosting.Abstractions (>= 10.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 1.0.0-rc.20 | 74 | 11/12/2026 |
| 1.0.0-rc.19 | 69 | 11/10/2026 |
| 1.0.0-rc.18 | 72 | 11/10/2026 |
| 1.0.0-rc.17 | 62 | 11/10/2026 |
| 1.0.0-rc.16 | 64 | 11/8/2026 |
| 1.0.0-rc.15 | 66 | 11/8/2026 |
| 1.0.0-rc.14 | 61 | 11/8/2026 |
| 1.0.0-rc.13 | 66 | 11/7/2026 |
| 1.0.0-rc.12 | 59 | 11/7/2026 |
| 1.0.0-rc.11 | 63 | 11/7/2026 |
| 1.0.0-rc.10 | 62 | 11/6/2026 |
| 1.0.0-rc.9 | 71 | 11/5/2026 |
| 1.0.0-rc.8 | 70 | 11/5/2026 |
| 1.0.0-rc.7 | 73 | 11/5/2026 |
| 1.0.0-rc.6 | 67 | 11/5/2026 |
| 1.0.0-rc.5 | 72 | 11/4/2026 |