PollyEFCore 1.0.0
See the version list below for details.
dotnet add package PollyEFCore --version 1.0.0
NuGet\Install-Package PollyEFCore -Version 1.0.0
<PackageReference Include="PollyEFCore" Version="1.0.0" />
<PackageVersion Include="PollyEFCore" Version="1.0.0" />
<PackageReference Include="PollyEFCore" />
paket add PollyEFCore --version 1.0.0
#r "nuget: PollyEFCore, 1.0.0"
#:package PollyEFCore@1.0.0
#addin nuget:?package=PollyEFCore&version=1.0.0
#tool nuget:?package=PollyEFCore&version=1.0.0
PollyEFCore
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
Automatic interception (recommended)
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.
Related packages
| Package | Downloads | Description |
|---|---|---|
| PollyMediatR | Polly v8 pipelines for MediatR request handlers | |
| PollyBackoff | Jitter, linear & custom backoff for Polly v8 retry | |
| PollyChaos | Fault & latency injection (Simmy for Polly v8) | |
| PollyCaching | Cache-aside resilience strategy for Polly v8 | |
| PollyBulkhead | Bulkhead / concurrency limiter for Polly v8 | |
| PollyOpenTelemetry | OpenTelemetry metrics & tracing for Polly v8 |
License
MIT
| Product | Versions 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. |
-
net8.0
- Microsoft.EntityFrameworkCore.Relational (>= 8.0.28)
- Polly.Core (>= 8.7.0)
-
net9.0
- Microsoft.EntityFrameworkCore.Relational (>= 8.0.28)
- Polly.Core (>= 8.7.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
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.