ThreadSafeDbContext 8.0.1

dotnet add package ThreadSafeDbContext --version 8.0.1
                    
NuGet\Install-Package ThreadSafeDbContext -Version 8.0.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="ThreadSafeDbContext" Version="8.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ThreadSafeDbContext" Version="8.0.1" />
                    
Directory.Packages.props
<PackageReference Include="ThreadSafeDbContext" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ThreadSafeDbContext --version 8.0.1
                    
#r "nuget: ThreadSafeDbContext, 8.0.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.
#:package ThreadSafeDbContext@8.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ThreadSafeDbContext&version=8.0.1
                    
Install as a Cake Addin
#tool nuget:?package=ThreadSafeDbContext&version=8.0.1
                    
Install as a Cake Tool

ThreadSafeDbContext

A thread safe Entity Framework DbContext implementation

Install

To use these extensions, install the nuget package for your C# project and ensure that the appropriate namespaces are referenced. Make sure that you have the necessary dependencies and target framework version set correctly.

dotnet add package ThreadSafeDbContext

Usages

Simply make your application DbContext inherit from ThreadSafeDbContext and you are good to go. The ThreadSafeDbContext class is a wrapper around the DbContext class and provides thread safe access to the DbContext class.

public class MyDbContext : ThreadSafeDbContext
{
    public MyDbContext(DbContextOptions<MyDbContext> options) : base(options)
    {
    }
}

// or

public class MyDbContext : ThreadSafeDbContext
{
    public MyDbContext()
    {
    }
    
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("connection string");
        base.OnConfiguring(optionsBuilder); // don't forget to call the base method
    }
}

Pros and cons

Pros

  • Thread safe access to the DbContext class
  • Standard compliant for the DbContext class
  • Use the DbSet and Queryable properties as you would normally do

Cons

  • The thread safety comes at a cost of performance. The performance hit is not significant but it is there.
  • The project is not an official implementation from Microsoft. It is a custom implementation, and may not be ready for production use.

Performance benchmark

Method Mean Error StdDev
Standard DbContext 280.6 us 4.30 us 4.41 us
ThreadSafe DbContext 291.1 us 4.48 us 5.16 us

More about

You can find more details about the implementation here: A Threadsafe implementation of DbContext

Feel free to explore and leverage these extensions.

Note: This code snippet is a standalone C# implementation and can be integrated into your project to extend the DbContext

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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.1 5,152 1/20/2025
8.0.1-preview.2 146 10/15/2024
8.0.0 4,632 4/26/2024
1.0.7 5,613 10/13/2023
1.0.6 230 10/12/2023
1.0.4 203 10/12/2023
1.0.3 210 10/12/2023
1.0.2 209 10/12/2023
1.0.1 197 10/12/2023
1.0.0 216 10/11/2023