Zejji.DbContextScope.EFCore 8.0.100.1

dotnet add package Zejji.DbContextScope.EFCore --version 8.0.100.1                
NuGet\Install-Package Zejji.DbContextScope.EFCore -Version 8.0.100.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="Zejji.DbContextScope.EFCore" Version="8.0.100.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Zejji.DbContextScope.EFCore --version 8.0.100.1                
#r "nuget: Zejji.DbContextScope.EFCore, 8.0.100.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.
// Install Zejji.DbContextScope.EFCore as a Cake Addin
#addin nuget:?package=Zejji.DbContextScope.EFCore&version=8.0.100.1

// Install Zejji.DbContextScope.EFCore as a Cake Tool
#tool nuget:?package=Zejji.DbContextScope.EFCore&version=8.0.100.1                

DbContextScope.EFCore

CI NuGet NuGet version (Zejji.DbContextScope.EFCore)

A library for managing the lifetime of Entity Framework Core DbContext instances.


Note re package versioning: Please use the version of this library which matches your EF Core version.

For EF Core 6, the NuGet package can be found at Zejji.DbContextScope.EFCore6.

To avoid having a separate NuGet package for each EF Core version, for EF Core 7 onwards, the decision was taken to remove the EF Core version from the package name and instead have the Zejji.DbContextScope.EFCore package version number follow the EF Core version - the current NuGet package can be found at Zejji.DbContextScope.EFCore.


This package is based on the original DbContextScope repository by Mehdi El Gueddari with the following changes:

  • updated for .NET 6+ and EF Core (including replacing usages of CallContext with AsyncLocal);
  • added fix for RefreshEntitiesInParentScope method so that it works correctly for entities with composite primary keys;
  • added fix for DbContextCollection's Commit and CommitAsync methods so that SaveChanges can be called more than once if there is a DbUpdateConcurrencyException (see this unmerged pull request in the original DbContextScope repository);
  • added the RegisteredDbContextFactory class as a concrete implementation of the IDbContextFactory interface, which allows users to easily register factory functions for one or more DbContext type(s) during startup; and
  • added unit tests.

Description

Mehdi El Gueddari's original article describing the thinking behind the DbContextScope library can be found here.

In summary, the library addresses the problem that injecting DbContext instances as a scoped dependency (which ordinarily results in one instance per web request) offers insufficient control over the lifetime of DbContext instances in more complex scenarios.

The DbContextScope library allows users to create scopes which control the lifetime of ambient DbContext instances, as well giving control over the exact time at which changes are saved.

For general usage instructions, see article referred to above and the original GitHub repository readme file (a copy of which is included in this repository here). Please note the Mehdime.Entity namespace has been renamed to Zejji.Entity.

The new RegisteredDbContextFactory class can be used as follows:

  • In Startup.cs, register a RegisteredDbContextFactory instance as a singleton and register one or more DbContext factory functions on that instance, e.g.:
using Zejji.Entity;
...
public void ConfigureServices(IServiceCollection services)
{
    ...
    // Create an instance of the RegisteredDbContextFactory
    var dbContextFactory = new RegisteredDbContextFactory();

    // Register factory functions for each of the required DbContext types
    dbContextFactory.RegisterDbContextType<DbContextOne>(() =>
        new DbContextOne(Configuration.GetConnectionString("DatabaseOne")));
    dbContextFactory.RegisterDbContextType<DbContextTwo>(() =>
        new DbContextTwo(Configuration.GetConnectionString("DatabaseTwo")));

    // Register the RegisteredDbContextFactory instance as a singleton
    // with the dependency injection container.
    services.AddSingleton<IDbContextFactory>(dbContextFactory);
    ...
}

See also the unit tests for RegisteredDbContextFactory here.

Getting Started

Dependencies

  • .NET 6+
  • EF Core (version equal to or higher than Zejji.DbContextScope.EFCore package version)

Installing

  • dotnet add package Zejji.DbContextScope.EFCore

License

This project is licensed under the MIT License - see the LICENSE.txt file for details

Acknowledgments

Many thanks to Mehdi El Gueddari for creating the original DbContextScope library.

Product Compatible and additional computed target framework versions.
.NET 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. 
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
8.0.100.1 1,555 11/20/2023
8.0.100-rc.2 87 10/31/2023
7.0.5 366 4/28/2023
7.0.4 207 3/31/2023
7.0.3 251 2/14/2023
7.0.2 247 2/14/2023