Reyrb.Database.ForUpdate 10.0.0

The owner has unlisted this package. This could mean that the package is deprecated, has security vulnerabilities or shouldn't be used anymore.
dotnet add package Reyrb.Database.ForUpdate --version 10.0.0
                    
NuGet\Install-Package Reyrb.Database.ForUpdate -Version 10.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="Reyrb.Database.ForUpdate" Version="10.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Reyrb.Database.ForUpdate" Version="10.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Reyrb.Database.ForUpdate" />
                    
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 Reyrb.Database.ForUpdate --version 10.0.0
                    
#r "nuget: Reyrb.Database.ForUpdate, 10.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 Reyrb.Database.ForUpdate@10.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=Reyrb.Database.ForUpdate&version=10.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Reyrb.Database.ForUpdate&version=10.0.0
                    
Install as a Cake Tool

Reyrb.Database.ForUpdate

PostgreSQL row locking for EF Core 10 / .NET 10, adapted from the tag-based PostgresQueryExtensions and SelectForUpdateCommandInterceptor in lootbucks-backend.

Register once when configuring your context:

using Microsoft.EntityFrameworkCore;
using Reyrb.Database.ForUpdate;

services.AddDbContext<AppDbContext>(options =>
    options.UseNpgsql(connectionString).UseForUpdate());

Alternatively, register new SelectForUpdateCommandInterceptor() with EF Core's AddInterceptors. The interceptor is stateless and supports pooled contexts.

Keep the read and subsequent update in the same explicit transaction:

await using var transaction = await context.Database.BeginTransactionAsync(cancellationToken);

var wallet = await context.Wallets
    .Where(x => x.Id == walletId)
    .ForUpdate()
    .SingleAsync(cancellationToken);

wallet.Balance -= amount;
await context.SaveChangesAsync(cancellationToken);
await transaction.CommitAsync(cancellationToken);

Use .ForUpdate(skipLocked: true) (or .ForUpdate(true)) to omit rows locked by another transaction, for example when claiming queued work. Locks last until commit or rollback; without an explicit transaction they end with the statement.

Both synchronous and asynchronous reads are supported. The extension adds the FOR UPDATE or FOR UPDATE SKIP LOCKED tag; the interceptor appends the SQL clause when executing the command. Other TagWith tags can appear before or after it. If both lock tags occur, SKIP LOCKED takes precedence. Repeated interception does not duplicate the clause. ToQueryString() shows the tag, not the final clause.

Include(...).ForUpdate().ThenInclude(...) chaining is supported, but PostgreSQL's SQL restrictions still apply. This is a command-level suffix, with no OF table targeting: joins may lock multiple tables, and nullable outer joins, aggregates, DISTINCT, grouping, and set operations may reject FOR UPDATE. For complex loads, lock the root rows with a simple query first, then load related data in the same transaction. It is not a general SQL rewriter for raw multi-statement batches. Do not use these tags with ExecuteUpdate or ExecuteDelete.

Use a PostgreSQL provider such as Npgsql.EntityFrameworkCore.PostgreSQL. SQL Server, SQLite, and the EF in-memory provider are not supported for row locking. The library depends only on EF Core Relational; install your provider separately.

Tests require Docker and run against an isolated PostgreSQL 16.3 container:

dotnet test Reyrb.Database.ForUpdate.Tests --configuration Release
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