zejji / DbContextScopeEFCore

A version of Mehdi El Gueddari's DbContextScope library updated for EF Core with a number of improvements and bug fixes
9 stars 2 forks source link

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:

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:

See also the unit tests for RegisteredDbContextFactory here.

Getting Started

Dependencies

Installing

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.