Shuttle.Extensions.EFCore
20.0.2
Prefix Reserved
dotnet add package Shuttle.Extensions.EFCore --version 20.0.2
NuGet\Install-Package Shuttle.Extensions.EFCore -Version 20.0.2
<PackageReference Include="Shuttle.Extensions.EFCore" Version="20.0.2" />
<PackageVersion Include="Shuttle.Extensions.EFCore" Version="20.0.2" />
<PackageReference Include="Shuttle.Extensions.EFCore" />
paket add Shuttle.Extensions.EFCore --version 20.0.2
#r "nuget: Shuttle.Extensions.EFCore, 20.0.2"
#:package Shuttle.Extensions.EFCore@20.0.2
#addin nuget:?package=Shuttle.Extensions.EFCore&version=20.0.2
#tool nuget:?package=Shuttle.Extensions.EFCore&version=20.0.2
Shuttle.Extensions.EFCore
PM> Shuttle.Extensions.EFCore
Extensions for Entity Framework Core.
Configuration
services.AddSingleton<IDbContextService, DbContextService>();
Usage
The DbContextService is a simple service that provides a DbContext instance. This is useful when you need to create a DbContext instance in a service that is not registered with the service collection and you need to use the same DbContext in multiple dependencies without having to use the DbContextFactory in each.
private readonly IDbContextFactory<RequiredDbContext> _dbContextFactory;
private readonly IDbContextService _dbContextService;
private readonly IDependencyA _dependencyA;
private readonly IDependencyB _dependencyB;
public Processor(IDbContextService dbContextService, IDbContextFactory<RequiredDbContext> dbContextFactory, IDependencyA dependencyA, IDependencyB dependencyB)
{
_dbContextService = Guard.AgainstNull(dbContextService);
_dbContextFactory = Guard.AgainstNull(dbContextFactory);
_dependencyA = Guard.AgainstNull(dependencyA);
_dependencyB = Guard.AgainstNull(dependencyB);
}
public async Task ExecuteAsync(CancellationToken cancellationToken = default)
{
using (var tx = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled))
await using (var dbContext = await _dbContextFactory.CreateDbContextAsync(cancellationToken))
using (_dbContextService.Add(dbContext))
{
_dependencyA.Process();
_dependencyB.Process();
tx.Complete();
}
}
The dependencies IDependencyA and IDependencyB can now use the DbContext instance that was created in the Processor class:
public class DependencyA : IDependencyA
{
private readonly IDbContextService _dbContextService;
p
ublic DependencyA(IDbContextService dbContextService)
{
_dbContextService = Guard.AgainstNull(dbContextService);
}
public void Process()
{
var dbContext = _dbContextService.Get<RequiredDbContext>();
// use the dbContext instance
}
}
There is also a DbContextScope that can be used to create a new scope within an asynchronous flow. Please see the ScopeFixture for more information.
| Product | Versions 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. |
-
- Microsoft.EntityFrameworkCore.SqlServer (>= 8.0.11)
- Shuttle.Core.Contract (>= 20.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Shuttle.Extensions.EFCore:
| Package | Downloads |
|---|---|
|
Shuttle.Extensions.EFCore.ThreadDbContextScope
Create a new DbContextScope per processor thread. |
GitHub repositories
This package is not used by any popular GitHub repositories.