Fix DbContext concurrency error in parallel company verification
Use IDbContextFactory pattern to create isolated DbContext instances for each cache operation, making parallel verification thread-safe. Changes: - Add IDbContextFactory<ApplicationDbContext> registration - Update CompanyVerifierService to use factory for cache operations - Update tests with InMemoryDatabaseRoot for shared test data Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,6 +19,19 @@ public static class DependencyInjection
|
||||
public static IServiceCollection AddInfrastructure(this IServiceCollection services, IConfiguration configuration)
|
||||
{
|
||||
// Configure DbContext with SQL Server
|
||||
// AddDbContextFactory enables thread-safe parallel operations
|
||||
services.AddDbContextFactory<ApplicationDbContext>(options =>
|
||||
options.UseSqlServer(
|
||||
configuration.GetConnectionString("DefaultConnection"),
|
||||
sqlOptions =>
|
||||
{
|
||||
sqlOptions.EnableRetryOnFailure(
|
||||
maxRetryCount: 3,
|
||||
maxRetryDelay: TimeSpan.FromSeconds(30),
|
||||
errorNumbersToAdd: null);
|
||||
}));
|
||||
|
||||
// Also register DbContext for scoped injection (non-parallel scenarios)
|
||||
services.AddDbContext<ApplicationDbContext>(options =>
|
||||
options.UseSqlServer(
|
||||
configuration.GetConnectionString("DefaultConnection"),
|
||||
|
||||
Reference in New Issue
Block a user