title: A Command Is Already In Progress
body: <p>I am attempting to run a background worker for a web app that I am developing. I am using Npgsql as my EF Core provider.</p>
  
  <p>For clarification, I have injected my <code>DbContext</code> with a Transient lifetime, and have allowed Pooling in my connection string, however, whenever I try to test it I get the following error: </p>
  
  <blockquote>
    <p>Npgsql.NpgsqlOperationInProgressException: A command is already in progress: [My Query Here]</p>
  </blockquote>
  
  <p>I have my <code>Program</code> Class Set up as such:</p>
  
  <pre><code>public class Program
  {
      public static void Main(string[] args)
      {
          CreateHostBuilder(args).Build().Run();
      }
  
      public static IHostBuilder CreateHostBuilder(string[] args) =&gt;
          Host.CreateDefaultBuilder(args)
              .ConfigureServices((hostContext, services) =&gt;
              {
                  // Get the configuration
                  IConfiguration config = hostContext.Configuration;
  
                  // DbContext
                  services.AddDbContext&lt;MyDbContext&gt;(options =&gt; options.UseNpgsql(config.GetConnectionString("PostgreSQLString")), ServiceLifetime.Transient);
  
                  services.AddHostedService&lt;Worker&gt;();
                  services.AddScoped&lt;IDTOService, BackgroundDTOService&gt;();
              });
  }
  </code></pre>
  
  <p>Which then leads to my <code>Worker</code> class</p>
  
  <pre><code>public class Worker : BackgroundService
  {
      private Logger logger;
  
      public Worker(IServiceProvider services, IConfiguration configuration)
      {
          this.Services = services;
  
          var optionsBuilder = new DbContextOptionsBuilder&lt;MyDbContext&gt;();
          optionsBuilder.UseNpgsql(configuration.GetConnectionString("PostgreSQLString"));
          var context = new StatPeekContext(optionsBuilder.Options);
  
          this.logger = new Logger(new LogWriter(context));
      }
  
      public IServiceProvider Services { get; }
  
      protected override async Task ExecuteAsync(CancellationToken stoppingToken)
      {
          this.logger.LogInformation("ExecuteAsync in Worker Service is running.");
          await this.DoWork(stoppingToken);
      }
  
      private async Task DoWork(CancellationToken stoppingToken)
      {
          using (var scope = Services.CreateScope())
          {
              var context = scope.ServiceProvider.GetRequiredService&lt;MyDbContext&gt;();
              var dtoService = scope.ServiceProvider.GetRequiredService&lt;IDTOService&gt;();
              await dtoService.ProcessJSON(stoppingToken);
          }
      }
  
      public override async Task StopAsync(CancellationToken cancellationToken)
      {
          this.logger.LogInformation("Worker service is stopping.");
          await Task.CompletedTask;
      }
  }
  </code></pre>
  
  <p>which leads to my <code>BackGroundDTOService</code></p>
  
  <pre><code>public class BackgroundDTOService : IDTOService
  {
      private int executionCount = 0;
      private Logger logger;
      private MyDbContext context;
      private DbContextOptionsBuilder&lt;MyDbContext&gt; optionsBuilder;
  
      public BackgroundDTOService(IConfiguration configuration, MyDbContext context)
      {
          this.optionsBuilder = new DbContextOptionsBuilder&lt;MyDbContext&gt;();
          this.optionsBuilder.UseNpgsql(configuration.GetConnectionString("PostgreSQLString"));
  
          this.logger = new Logger(new LogWriter(new MyDbContext(this.optionsBuilder.Options)));
  
          this.context = context;
      }
  
      public async Task ProcessJSON(CancellationToken stoppingToken)
      {
          while (!stoppingToken.IsCancellationRequested)
          {
              this.executionCount++;
  
              this.logger.LogInformation($"DTO Service is working. Count: {this.executionCount}");
  
              this.ProcessTeams();
  
              await Task.Delay(TimeSpan.FromSeconds(1), stoppingToken);
          }
      }
  
      public void ProcessTeams()
      {
          // Add Any Franchises that don't exist
          var franchiseDumps = this.context.RequestDumps.Where(rd =&gt; rd.Processed == false &amp;&amp; rd.DumpType == "leagueteams");
          foreach (RequestDump teamDump in franchiseDumps)
          {
              var league = this.context.Leagues.Include(l =&gt; l.Franchises).FirstOrDefault(l =&gt; l.Id == teamDump.LeagueID);
              var teams = Jeeves.GetJSONFromKey&lt;List&lt;DTOTeam&gt;&gt;(teamDump.JsonDump, "leagueTeamInfoList");
  
              foreach (DTOTeam team in teams)
              {
                  this.UpdateFranchise(team, league);
              }
  
              this.logger.LogInformation($"DTO Service Processed League Teams on count {this.executionCount}");
          }
  
          this.context.SaveChanges();
      }
  </code></pre>
  
  <p>The error appears to occur immediately after snagging <code>franchiseDumps</code> when it tries to get <code>league</code></p>