Library for managing DbContext the right way with Entity Framework Core.
This package is based on the original DbContextScope repository by Mehdi El Gueddari with the following changes:
- projects were updated to .NET 6+ and Entity Framework Core;
- usages of
CallContext
were replaced withAsyncLocal
; - added fix for
RefreshEntitiesInParentScope
method so that it works correctly for entities with composite primary keys; - added fix for
DbContextCollection
'sCommit
andCommitAsync
methods so thatSaveChanges
can be called more than once if there is aDbUpdateConcurrencyException
(see this unmerged pull request in the originalDbContextScope
repository); - added the
RegisteredDbContextFactory
class as a concrete implementation of theIDbContextFactory
interface, which allows users to easily register factory functions for one or moreDbContext
type(s) during startup; and - added unit tests.
Library provides simple and flexible way to manage your Entity Framework Core DbContext instances.
DbContextScope
was created out of the need for a better way to manage DbContext instances in Entity Framework-based applications.
The commonly advocated method of injecting DbContext instances works fine for single-threaded web applications where each web request implements exactly one business transaction. But it breaks down quite badly when console apps, Windows Services, parallelism and requests that need to implement multiple independent business transactions make their appearance.
The alternative of manually instantiating DbContext instances and manually passing them around as method parameters is (speaking from experience) more than cumbersome.
DbContextScope
implements the ambient context pattern for DbContext instances. It doesn't force any particular design pattern or application architecture to be used. It works beautifully with dependency injection and works like a charm without any IoC container.
And most importantly, DbContextScope
has been battle-tested in a large-scale applications for a long time.
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. Copy of original README with improved formatting and fixing broken links is included in this repository here.
Please note the Mehdime.Entity
namespace has been renamed to Slant.Entity
due to naming conventions in Slant packages.
The new RegisteredDbContextFactory
class can be used as follows:
- In
Startup.cs
, register aRegisteredDbContextFactory
instance as a singleton and register one or moreDbContext
factory functions on that instance, e.g.:
using Slant.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.
- .NET 8+
- Entity Framework Core 8+
dotnet add package Slant.Entity
Many thanks to Mehdi El Gueddari for creating the original DbContextScope
library.