TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore 3.0.0-preview.2

This is a prerelease version of TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore.
dotnet add package TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore --version 3.0.0-preview.2
                    
NuGet\Install-Package TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore -Version 3.0.0-preview.2
                    
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="TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore" Version="3.0.0-preview.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore" Version="3.0.0-preview.2" />
                    
Directory.Packages.props
<PackageReference Include="TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore" />
                    
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 TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore --version 3.0.0-preview.2
                    
#r "nuget: TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore, 3.0.0-preview.2"
                    
#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 TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore@3.0.0-preview.2
                    
#: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=TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore&version=3.0.0-preview.2&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore&version=3.0.0-preview.2&prerelease
                    
Install as a Cake Tool

TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore

ASP.NET Core integration for the unit of work from TailoredApps.Shared.EntityFramework. An MVC action filter ends every controller action by committing the unit of work, or by rolling it back when the action failed, so controllers do not commit or roll back by hand.

Installation

dotnet add package TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore

Registration

using TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore;

// AppDbContext : DbContext, IAppDbContext
builder.Services.AddDbContext<AppDbContext>(options => options.UseSqlServer(connectionString));
builder.Services.AddUnitOfWorkForWebApi<IAppDbContext, AppDbContext>();

builder.Services.AddControllers(options => options.Filters.AddUnitOfWorkTransactionAttribute());

AddUnitOfWorkForWebApi<TInterface, TContext>() registers IUnitOfWork, IUnitOfWork<TInterface>, the context interface and TransactionFilterAttribute, all scoped. It returns IUnitOfWorkOptionsBuilder, on which the save and transaction hooks of TailoredApps.Shared.EntityFramework are added. AddUnitOfWorkTransactionAttribute() adds the filter to every controller action.

Usage

using TailoredApps.Shared.EntityFramework.Interfaces.UnitOfWork;

[ApiController]
[Route("api/orders")]
public class OrdersController(IUnitOfWork<IAppDbContext> unitOfWork) : ControllerBase
{
    [HttpPost]
    public IActionResult Create(CreateOrder command)
    {
        if (command.Lines.Count == 0)
        {
            return BadRequest();   // 4xx result: the filter rolls back
        }

        unitOfWork.DataProvider.Orders.Add(new Order { Number = command.Number });
        return Ok();               // the filter saves the pending changes and commits
    }
}

An action can ask for another isolation level:

using System.Data;
using TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore.Attributes;

[HttpPost("transfer")]
[TransactionIsolationLevel(IsolationLevel.Serializable)]
public async Task<IActionResult> Transfer(TransferFunds command) { /* ... */ }

Good to know

  • The filter rolls back when the action threw or returned a result with a status code of 400 or higher (BadRequest(), Conflict(), StatusCode(500)). A rollback also detaches every tracked entity.
  • Otherwise it calls CommitTransaction(), which saves pending changes first; an action does not have to call SaveChanges itself.
  • The transaction is opened lazily: by the first SaveChanges / SaveChangesAsync, or by BeginTransactionManually(). Queries executed before that run outside the transaction.
  • When the commit fails the filter rolls back and reports the commit exception as the action's exception (an AggregateException with both when the rollback fails too).
  • [TransactionIsolationLevel] is read from the action method only; placed on a controller class it has no effect. The default level is ReadCommitted. The unit of work supports ReadCommitted, ReadUncommitted and Serializable; another level throws ArgumentOutOfRangeException when the connection is opened.
  • The DbContext is resolved from the container, so it has to be registered (AddDbContext).

Documentation

https://shared.tailoredapps.pl/en/Libraries/EntityFramework/UnitOfWork.WebApiCore/

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.  net11.0 is compatible. 
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
3.0.0-preview.2 47 11/12/2026
3.0.0-preview.1 57 11/12/2026
2.1.0 97 11/12/2026
2.0.1 90 11/12/2026
2.0.0 103 11/12/2026
1.6.0 94 11/12/2026
1.4.19 427 10/26/2026
1.4.18 573 10/23/2026
1.4.17 952 10/6/2026
1.4.16 115 9/28/2026
1.4.15 120 9/21/2026
1.4.14 129 8/20/2026
1.4.13 124 8/20/2026
1.4.12 132 8/20/2026
1.4.11 132 8/7/2026
1.4.10 248 7/8/2026
1.4.9 146 7/8/2026
1.4.8 143 7/6/2026
1.4.7 156 5/20/2026
1.4.6 142 5/20/2026
Loading failed