MethodCache.EntityFrameworkCore 1.0.0-rc.14

This is a prerelease version of MethodCache.EntityFrameworkCore.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package MethodCache.EntityFrameworkCore --version 1.0.0-rc.14
                    
NuGet\Install-Package MethodCache.EntityFrameworkCore -Version 1.0.0-rc.14
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MethodCache.EntityFrameworkCore" Version="1.0.0-rc.14" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MethodCache.EntityFrameworkCore" Version="1.0.0-rc.14" />
                    
Directory.Packages.props
<PackageReference Include="MethodCache.EntityFrameworkCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MethodCache.EntityFrameworkCore --version 1.0.0-rc.14
                    
#r "nuget: MethodCache.EntityFrameworkCore, 1.0.0-rc.14"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MethodCache.EntityFrameworkCore@1.0.0-rc.14
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MethodCache.EntityFrameworkCore&version=1.0.0-rc.14&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=MethodCache.EntityFrameworkCore&version=1.0.0-rc.14&prerelease
                    
Install as a Cake Tool

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 SaveChangesAsync invalidates 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. TransactionScope works 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 that ON DELETE CASCADE removes with it, while a user saved on every sign-in clears nothing.
  • A warning where it can't help. ExecuteUpdate and ExecuteDelete on 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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