IdempotencyShield.EntityFrameworkCore 1.0.1

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

IdempotencyShield.EntityFrameworkCore

Entity Framework Core storage implementation for the IdempotencyShield library. Allows you to store idempotency records and distributed locks in SQL Server, PostgreSQL, MySQL, or any other database supported by EF Core.

Installation

dotnet add package IdempotencyShield.EntityFrameworkCore

Quick Start

1. Create your DbContext

You don't need to inherit from a special base class. Just make sure your specific DbContext has the required tables. You can achieve this by using our entities or configuring them manually.

Option A: Dedicated Context (Recommended)

using IdempotencyShield.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;

public class MyIdempotencyContext : DbContext
{
    public MyIdempotencyContext(DbContextOptions<MyIdempotencyContext> options) 
        : base(options) { }

    // Define the datasets
    public DbSet<IdempotencyRecordEntity> IdempotencyRecords { get; set; }
    public DbSet<IdempotencyLockEntity> IdempotencyLocks { get; set; }
}

2. Register Services

In Program.cs:

using IdempotencyShield.Extensions;
using Microsoft.EntityFrameworkCore;

var builder = WebApplication.CreateBuilder(args);

// 1. Register EF Core Context
builder.Services.AddDbContext<MyIdempotencyContext>(options =>
    options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")));

// 2. Add IdempotencyShield with EF Core storage
builder.Services.AddIdempotencyShieldWithEfCore<MyIdempotencyContext>();

var app = builder.Build();

app.UseIdempotencyShield();
app.MapControllers();
app.Run();

3. Create Migrations

Run standard EF Core commands to create the tables:

dotnet ef migrations add InitialCreate --context MyIdempotencyContext
dotnet ef database update --context MyIdempotencyContext

This will create two tables:

  • IdempotencyRecords: Stores cached responses.
  • IdempotencyLocks: Handles distributed locking.

Advanced Configuration

Using an Existing DbContext

If you want to add idempotency tables to your existing application DbContext:

public class AppDbContext : DbContext
{
    public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

    // Your app entities...
    public DbSet<Product> Products { get; set; }

    // Idempotency entities
    public DbSet<IdempotencyRecordEntity> IdempotencyRecords { get; set; }
    public DbSet<IdempotencyLockEntity> IdempotencyLocks { get; set; }
}

Then register it normally:

builder.Services.AddIdempotencyShieldWithEfCore<AppDbContext>();

Supported Databases

Since this depends only on Microsoft.EntityFrameworkCore, it works with:

  • SQL Server
  • PostgreSQL (Npgsql)
  • MySQL / MariaDB (Pomelo)
  • SQLite
  • Oracle
  • ...and more!

Database Schema

The library uses two entities:

1. IdempotencyRecordEntity

Stores the cached response.

  • Key (PK, String): The idempotency key.
  • StatusCode (Int): HTTP status code.
  • HeaderJson (String): Serialized headers.
  • Body (Byte[]): Response body.
  • CreatedAt (DateTime): Timestamp.
  • ExpiresAt (DateTime): Expiration time.
  • BodyHash (String, Nullable): SHA256 of the request payload.

2. IdempotencyLockEntity

Manages distributed locks.

  • Key (PK, String): The key being processed.
  • AcquiredAt (DateTime): When the lock was taken.
  • ExpiresAt (DateTime): Safety expiration to prevent deadlocks.

Cleanup Strategy

The database will grow over time as new keys are used. You should implement a background job (e.g., using IHostedService or Hangfire) to delete expired records.

-- Example SQL Cleanup
DELETE FROM IdempotencyRecords WHERE ExpiresAt < GETUTCDATE();
DELETE FROM IdempotencyLocks WHERE ExpiresAt < GETUTCDATE();

License

MIT License - See LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 was computed.  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 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.2 522 12/11/2025
1.0.1 484 12/10/2025
1.0.0 500 12/10/2025