Slant.Entity
7.0.5
See the version list below for details.
dotnet add package Slant.Entity --version 7.0.5
NuGet\Install-Package Slant.Entity -Version 7.0.5
<PackageReference Include="Slant.Entity" Version="7.0.5" />
paket add Slant.Entity --version 7.0.5
#r "nuget: Slant.Entity, 7.0.5"
// Install Slant.Entity as a Cake Addin #addin nuget:?package=Slant.Entity&version=7.0.5 // Install Slant.Entity as a Cake Tool #tool nuget:?package=Slant.Entity&version=7.0.5
Slant.Entity
Library for managing DbContext the right way with Entity Framework Core.
Overview
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.
Description
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.
Dependencies
- .NET 6+
- Entity Framework Core 7
Installation
dotnet add package Slant.Entity
Acknowledgments
Many thanks to Mehdi El Gueddari for creating the original DbContextScope
library.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. 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. |
-
net6.0
- Microsoft.EntityFrameworkCore (>= 7.0.5)
- Microsoft.EntityFrameworkCore.Relational (>= 7.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.