Repository: dotnet/efcore
File: test/EFCore.Relational.Tests/RelationalConnectionTest.cs (C#)

Code before:
            });

            // Dispose the scope on the main thread, which will trigger the TransactionCompleted event
            // The event handler (HandleTransactionCompleted) may execute on a different thread and race
            // with the ClearTransactions call in resetTask
            scope.Dispose();

            await resetTask;
        }

        // Test with ResetState() before scope.Complete()
        for (var i = 0; i < Environment.ProcessorCount; i++)
        {
            var connection = new FakeRelationalConnection(
                CreateOptions(new FakeRelationalOptionsExtension().WithConnectionString("Database=ConcurrencyTest")));

            var scope = new TransactionScope();
            connection.Open();

            // Start the reset task first
            var resetTask = Task.Run(() =>
            {
                // ResetState calls ClearTransactions which might race with HandleTransactionCompleted
                ((IResettableService)connection).ResetState();
            });

            // Complete and dispose the scope, which will trigger the TransactionCompleted event
            scope.Complete();
            scope.Dispose();

            await resetTask;
        }
    }

    private static IDbContextOptions CreateOptions(params RelationalOptionsExtension[] optionsExtensions)
    {
        var optionsBuilder = new DbContextOptionsBuilder();

        foreach (var optionsExtension in optionsExtensions)
        {
            ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(optionsExtension);
        }

        return optionsBuilder.Options;
    }

    private class FakeLoggingOptions(bool sensitiveDataLoggingEnabled, bool detailedErrorsEnabled = false) : ILoggingOptions
    {
        public void Initialize(IDbContextOptions options)
        {
        }

Review comment (bug):
@copilot Only for the second loop:
```suggestion
                // Small delay to increase chance of race condition
                await Task.Yield();

                // ResetState calls ClearTransactions which might race with HandleTransactionCompleted
```

Code after:
            });

            // Dispose the scope on the main thread, which will trigger the TransactionCompleted event
            // The event handler (HandleTransactionCompleted) may execute on a different thread and race
            // with the ClearTransactions call in resetTask
            scope.Dispose();

            await resetTask;
        }

        // Test with ResetState() before scope.Complete()
        for (var i = 0; i < Environment.ProcessorCount; i++)
        {
            var connection = new FakeRelationalConnection(
                CreateOptions(new FakeRelationalOptionsExtension().WithConnectionString("Database=ConcurrencyTest")));

            var scope = new TransactionScope();
            connection.Open();

            // Start the reset task first
            var resetTask = Task.Run(async () =>
            {
                // Small delay to increase chance of race condition
                await Task.Yield();

                // ResetState calls ClearTransactions which might race with HandleTransactionCompleted
                ((IResettableService)connection).ResetState();
            });

            // Complete and dispose the scope, which will trigger the TransactionCompleted event
            scope.Complete();
            scope.Dispose();

            await resetTask;
        }
    }

    private static IDbContextOptions CreateOptions(params RelationalOptionsExtension[] optionsExtensions)
    {
        var optionsBuilder = new DbContextOptionsBuilder();

        foreach (var optionsExtension in optionsExtensions)
        {
            ((IDbContextOptionsBuilderInfrastructure)optionsBuilder).AddOrUpdateExtension(optionsExtension);
        }

        return optionsBuilder.Options;
    }

    private class FakeLoggingOptions(bool sensitiveDataLoggingEnabled, bool detailedErrorsEnabled = false) : ILoggingOptions
    {