Slant.Entity 8.0.7

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

// Install Slant.Entity as a Cake Tool
#tool nuget:?package=Slant.Entity&version=8.0.7                

Slant.Entity

Library for managing DbContext the right way with Entity Framework Core.

NuGet version (Slant.Entity) NuGet Build Status GitHub license

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 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

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 a RegisteredDbContextFactory instance as a singleton and register one or more DbContext 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 8+
  • Entity Framework Core 8+

Installation

dotnet add package Slant.Entity

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.7 62 7/29/2024
8.0.6 205 6/15/2024
8.0.2 137 3/2/2024
8.0.1 158 1/19/2024
7.0.12 237 10/18/2023
7.0.11 192 9/14/2023
7.0.10 181 9/10/2023
7.0.5 277 9/10/2023
1.1.0 6,730 4/13/2016
1.0.0 7,194 4/10/2016