TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore
2.1.0
See the version list below for details.
dotnet add package TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore --version 2.1.0
NuGet\Install-Package TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore -Version 2.1.0
<PackageReference Include="TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore" Version="2.1.0" />
<PackageVersion Include="TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore" Version="2.1.0" />
<PackageReference Include="TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore" />
paket add TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore --version 2.1.0
#r "nuget: TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore, 2.1.0"
#:package TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore@2.1.0
#addin nuget:?package=TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore&version=2.1.0
#tool nuget:?package=TailoredApps.Shared.EntityFramework.UnitOfWork.WebApiCore&version=2.1.0
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 callSaveChangesitself. - The transaction is opened lazily: by the first
SaveChanges/SaveChangesAsync, or byBeginTransactionManually(). 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
AggregateExceptionwith 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 isReadCommitted. The unit of work supportsReadCommitted,ReadUncommittedandSerializable; another level throwsArgumentOutOfRangeExceptionwhen the connection is opened.- The
DbContextis resolved from the container, so it has to be registered (AddDbContext).
Documentation
https://shared.tailoredapps.pl/en/Libraries/EntityFramework/UnitOfWork.WebApiCore/
| Product | Versions 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. |
-
net10.0
- TailoredApps.Shared.EntityFramework (>= 2.1.0)
-
net11.0
- TailoredApps.Shared.EntityFramework (>= 2.1.0)
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 |