PollyEFCore 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package PollyEFCore --version 1.0.0
                    
NuGet\Install-Package PollyEFCore -Version 1.0.0
                    
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="PollyEFCore" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="PollyEFCore" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="PollyEFCore" />
                    
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 PollyEFCore --version 1.0.0
                    
#r "nuget: PollyEFCore, 1.0.0"
                    
#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 PollyEFCore@1.0.0
                    
#: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=PollyEFCore&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=PollyEFCore&version=1.0.0
                    
Install as a Cake Tool

PollyEFCore

NuGet NuGet Downloads CI

Polly v8 resilience pipelines for Entity Framework Core — automatically wrap every EF Core command (queries, SaveChanges, scalar operations) with retry, timeout, circuit-breaker and more. Handles transient database errors, connection blips and SQL timeouts without changing a single line of handler or repository code.

services.AddDbContext<AppDbContext>(options =>
    options
        .UseSqlServer(connectionString)
        .AddPollyResilience(pipeline =>
            pipeline.AddRetry(new RetryStrategyOptions
            {
                MaxRetryAttempts = 3,
                Delay = TimeSpan.FromMilliseconds(200),
                BackoffType = DelayBackoffType.Exponential,
                ShouldHandle = new PredicateBuilder().Handle<Exception>(),
            })));

Every query and SaveChangesAsync() call is now automatically retried on transient failure — no changes to your DbContext, repositories, or handlers.


Why PollyEFCore?

EF Core's built-in EnableRetryOnFailure() only handles SQL Azure connection failures. PollyEFCore gives you the full power of Polly v8 for any EF Core provider.

Feature EnableRetryOnFailure() PollyEFCore
Provider SQL Server / Azure SQL only Any provider (Postgres, MySQL, SQLite…)
Retry strategy Fixed with jitter Any Polly strategy (exponential, linear, custom)
Timeout ✅ per-command timeout
Circuit breaker ✅ stop hammering a broken DB
Hedging ✅ parallel speculative queries
Observability ✅ via PollyOpenTelemetry
Exception filter Hardcoded SQL error codes Any predicate

Installation

dotnet add package PollyEFCore

Targets net8.0 and net9.0 (requires EF Core 8+).

Dependencies: Polly.Core 8.*, Microsoft.EntityFrameworkCore.Relational 8.*


Quick start

Register once on DbContextOptionsBuilder — all commands are wrapped automatically:

// Program.cs / Startup.cs
services.AddDbContext<AppDbContext>(options =>
    options
        .UseNpgsql(connectionString)           // works with any provider
        .AddPollyResilience(pipeline =>
            pipeline
                .AddRetry(new RetryStrategyOptions
                {
                    MaxRetryAttempts = 3,
                    Delay = TimeSpan.FromMilliseconds(100),
                    BackoffType = DelayBackoffType.Exponential,
                    ShouldHandle = new PredicateBuilder().Handle<Exception>(),
                })
                .AddTimeout(TimeSpan.FromSeconds(30))));

Use your DbContext exactly as before — no code changes required:

// Repository — unchanged
public async Task<List<Product>> GetProductsAsync(CancellationToken ct)
    => await _context.Products.Where(p => p.Active).ToListAsync(ct); // retried automatically

public async Task SaveAsync(Product product, CancellationToken ct)
{
    _context.Products.Add(product);
    await _context.SaveChangesAsync(ct); // retried automatically
}

Explicit wrapping (for fine-grained control)

Use ExecuteWithResilienceAsync when you need different pipelines per operation, or when working inside an explicit transaction:

var products = await _context.Database.ExecuteWithResilienceAsync(
    _pipeline,
    ct => _context.Products.Where(p => p.Active).ToListAsync(ct),
    cancellationToken);
// Void overload for fire-and-forget style operations
await _context.Database.ExecuteWithResilienceAsync(
    _pipeline,
    async ct =>
    {
        _context.Orders.Add(order);
        await _context.SaveChangesAsync(ct);
    },
    cancellationToken);

ASP.NET Core example

var builder = WebApplication.CreateBuilder(args);

// Configure EF Core with Polly resilience
builder.Services.AddDbContext<ShopDbContext>(options =>
    options
        .UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection"))
        .AddPollyResilience(pipeline =>
            pipeline
                .AddRetry(new RetryStrategyOptions
                {
                    MaxRetryAttempts = 3,
                    Delay = TimeSpan.FromMilliseconds(200),
                    BackoffType = DelayBackoffType.Exponential,
                    ShouldHandle = new PredicateBuilder().Handle<Exception>(),
                })
                .AddTimeout(TimeSpan.FromSeconds(30))
                .AddCircuitBreaker(new CircuitBreakerStrategyOptions
                {
                    FailureRatio = 0.5,
                    MinimumThroughput = 10,
                    SamplingDuration = TimeSpan.FromSeconds(30),
                    BreakDuration = TimeSpan.FromSeconds(15),
                })));

Combining with PollyMediatR

Use with PollyMediatR for full stack resilience in CQRS apps:

// MediatR handler resilience (outer layer)
services.AddPollyMediatR(pipeline => pipeline.AddRetry(...));

// EF Core command resilience (inner layer)
services.AddDbContext<AppDbContext>(options =>
    options.UseSqlServer(cs).AddPollyResilience(pipeline => pipeline.AddRetry(...)));

⚠️ Transaction note

The automatic interceptor wraps individual ADO.NET commands. When using explicit DbTransaction objects, configure your pipeline to only handle connection-level failures (before command execution), or use ExecuteWithResilienceAsync for full unit-of-work retry control.


Package Downloads Description
PollyMediatR Downloads Polly v8 pipelines for MediatR request handlers
PollyBackoff Downloads Jitter, linear & custom backoff for Polly v8 retry
PollyChaos Downloads Fault & latency injection (Simmy for Polly v8)
PollyCaching Downloads Cache-aside resilience strategy for Polly v8
PollyBulkhead Downloads Bulkhead / concurrency limiter for Polly v8
PollyOpenTelemetry Downloads OpenTelemetry metrics & tracing for Polly v8

License

MIT

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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.3 207 8/7/2026
1.0.2 128 7/7/2026
1.0.1 123 6/23/2026
1.0.0 126 6/23/2026

1.0.0: Initial release. ResilienceDbCommandInterceptor wraps EF Core queries and SaveChanges with a Polly v8 ResiliencePipeline. AddPollyResilience() DI extension for DbContextOptionsBuilder. DatabaseFacade.ExecuteWithResilienceAsync() for explicit operation wrapping. Targets net8.0 and net9.0.