A thread safe Entity Framework DbContext implementation
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 ThreadSafeDbContextSimply 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
}
}- Thread safe access to the DbContext class
- Standard compliant for the DbContext class
- Use the DbSet and Queryable properties as you would normally do
- 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.
| 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 |
You can find more details about the implementation here: A Threadsafe implementation of DbContext
Feel free to explore and leverage these extensions.