Architect.EntityFramework.DbContextManagement 1.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Architect.EntityFramework.DbContextManagement --version 1.0.0
NuGet\Install-Package Architect.EntityFramework.DbContextManagement -Version 1.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="Architect.EntityFramework.DbContextManagement" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Architect.EntityFramework.DbContextManagement --version 1.0.0
#r "nuget: Architect.EntityFramework.DbContextManagement, 1.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.
// Install Architect.EntityFramework.DbContextManagement as a Cake Addin
#addin nuget:?package=Architect.EntityFramework.DbContextManagement&version=1.0.0

// Install Architect.EntityFramework.DbContextManagement as a Cake Tool
#tool nuget:?package=Architect.EntityFramework.DbContextManagement&version=1.0.0

Manage your DbContexts the right way.

https://github.com/TheArchitectDev/Architect.EntityFramework.DbContextManagement

The persistence layer or infrastructure layer uses the DbContext (e.g. from a repository). Controlling its scope and transaction lifetime, however, is ideally the reponsibility of the orchestrating layer (e.g. from an application service). This package adds that ability to Entity Framework Core 5.0.0 and up.

// Register
public void ConfigureServices(IServiceCollection services)
{
 services.AddPooledDbContextFactory<MyDbContext>(context =>
   context.UseSqlServer(connectionString, sqlServer => sqlServer.EnableRetryOnFailure()));

 services.AddDbContextScope<MyDbContext>();
}

// Consume
public class MyRepository : IMyRepository
{
 // Accesses the DbContext instance currently provided by the orchestrating layer
 private MyDbContext DbContext => this.DbContextAccessor.CurrentDbContext;

 private IDbContextAccessor<MyDbContext> DbContextAccessor { get; }

 public OrderRepo(IDbContextAccessor<MyDbContext> dbContextAccessor)
 {
   this.DbContextAccessor = dbContextAccessor ?? throw new ArgumentNullException(nameof(dbContextAccessor));
 }

 public Task<Order> GetOrderById(long id)
 {
   return this.DbContext.Orders.SingleOrDefaultAsync(o.Id == id);
 }
}

// Orchestrate
public class MyApplicationService
{
 private IDbContextProvider<MyDbContext> DbContextProvider { get; }
 private IMyRepository MyRepository { get; }

 public MyApplicationService(IDbContextProvider<MyDbContext> dbContextProvider, IMyRepository myRepository)
 {
   this.DbContextProvider = dbContextProvider ?? throw new ArgumentNullException(nameof(dbContextProvider));      
   this.MyRepository = myRepository ?? throw new ArgumentNullException(nameof(myRepository));
 }

 public async Task PerformSomeUnitOfWork()
 {
   // Provide a DbContext and execute a block of code within its scope
   await this.DbContextProvider.ExecuteInDbContextScopeAsync(async executionScope =>
   {
     // Until the end of this block, IDbContextAccessor can access the scoped DbContext
     // It can do so from any number of invocations deep (not shown here)
     await this.MyRepository.AddOrder(new Order());

     // If we have made modifications, we should save them
     // We could save here or as part of the repository methods, depending on our preference
     await executionScope.DbContext.SaveChangesAsync();
   }); // If no exceptions occurred and this scope was not nested in another, the transaction is committed asynchronously here
 }
}

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  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 was computed.  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. 
.NET Core netcoreapp3.1 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
2.0.0 4,711 3/23/2023
1.0.1 1,371 1/1/2022
1.0.0 1,384 12/18/2020