-
Notifications
You must be signed in to change notification settings - Fork 6.1k
Closed
Description
for ResilientTransaction class example.
public class ResilientTransaction
{
private DbContext _context;
private ResilientTransaction(DbContext context) =>
_context = context ?? throw new ArgumentNullException(nameof(context));
public static ResilientTransaction New (DbContext context) =>
new ResilientTransaction(context);
public async Task ExecuteAsync(Func<Task> action)
{
// Use of an EF Core resiliency strategy when using multiple DbContexts
// within an explicit BeginTransaction():
// https://docs.microsoft.com/ef/core/miscellaneous/connection-resiliency
var strategy = _context.Database.CreateExecutionStrategy();
await strategy.ExecuteAsync(async () =>
{
using (var transaction = _context.Database.BeginTransaction())
{
await action();
transaction.Commit();
}
});
}
}
How to cover Unit Test for ExecuteAsync. DO we need to mock context Database and Execution Strategy?
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
- ID: 57452ceb-e885-d193-d813-d7baf7291cbc
- Version Independent ID: 6ad8c3bc-fdef-1e24-175d-ca7c7b9a98ce
- Content: Implement resilient Entity Framework Core SQL connections
- Content Source: docs/architecture/microservices/implement-resilient-applications/implement-resilient-entity-framework-core-sql-connections.md
- Product: dotnet-architecture
- Technology: microservices
- GitHub Login: @nishanil
- Microsoft Alias: nanil