title: Correctly using of dependency injected DbContext to query Database
body: <p>I have set up my own context using (as I believe is correct):</p>
  
  <pre><code>public void ConfigureServices(IServiceCollection services)
  {
      services.AddDbContextPool&lt;JobTrackingContext&gt;(options =&gt; options.UseNpgsql(connection));
      services.AddScoped&lt;IJobRepository, JobRepository&gt;();
  }
  </code></pre>
  
  <p>Then I define my <code>JobTrackingContext</code> as follows:</p>
  
  <pre><code>public JobTrackingContext(DbContextOptions&lt;JobTrackingContext&gt; options)
              : base(options)
  {
      public DbSet&lt;Job&gt; Jobs { get; set; }
  }
  </code></pre>
  
  <p>Now I can define a repository to actually create/edit/delete Jobs:</p>
  
  <pre><code> public class JobRepository : GenericRepository&lt;Job, long&gt;, IJobRepository
  {
          private Job currentJob;
          public JobRepository(JobTrackingContext jobTrackingContext, JobTrackingSettings settings)
          : base(jobTrackingContext)
      {
          _settings = settings;
      }
          public async Task StartSync(JobType jobType, JobTriggerType jobTriggerType)
          {
              var tempJob = new Job(jobType, jobTriggerType);
              await _dbContext.Jobs.AddAsync(tempJob);
              await _dbContext.SaveChangesAsync();
          }
  }
  </code></pre>
  
  <p>And all this code gets instantiated by a Post-request to this API:</p>
  
  <pre><code>public async void Post()
  {
      _logger.LogDebug("Going to start account sync");
      await _jobRepository.StartSync(JobType.ZRequestSync, JobTriggerType.Scheduled);
      try
      {
          await _sync.StartAsync();
          await _jobRepository.ChangeSyncStatus(JobStatusType.Finished);
      }
      catch (Exception e)
      {
          _logger.LogError(e, "Error occured during sync :(");
          await _jobRepository.ChangeSyncStatus(JobStatusType.Failed);
      }
  }
  </code></pre>
  
  <p>Yet when I do this, I get an Exception with the message <code>Reset() called on connector with state Connecting</code>. I do not understand where this comes from.
   When I do not use the injected version, but instead do this:</p>
  
  <pre><code>using (var c = new JobTrackingContext())
  {
      var job = new Job(jobType, jobTriggerType)
      await c.Jobs.AddAsync(job);
      await c.SaveChangesAsync();
  }
  </code></pre>
  
  <p>All seems to be working fine. It seems that the context gets disposed too early. But how can I prevent this and/or what am I missing?</p>
  
  <p>The Full Stacktrace:</p>
  
  <pre><code>    System.ObjectDisposedException
    HResult=0x80131622
    Message=Cannot access a disposed object. A common cause of this error is disposing a context that was resolved from dependency injection and then later trying to use the same context instance elsewhere in your application. This may occur if you are calling Dispose() on the context, or wrapping the context in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.
    Source=Microsoft.EntityFrameworkCore
    StackTrace:
     at Microsoft.EntityFrameworkCore.DbContext.CheckDisposed()
     at Microsoft.EntityFrameworkCore.DbContext.get_DbContextDependencies()
     at Microsoft.EntityFrameworkCore.DbContext.&lt;SaveChangesAsync&gt;d__52.MoveNext()
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at System.Runtime.CompilerServices.TaskAwaiter1.GetResult()
     at ZShared.JobRepository.&lt;StartSync&gt;d__4.MoveNext() in C:\Users\richa\Documents\Codes\Company\Product\Shared\Folder\JobRepository.cs:line 38
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
     at ZAccountSyncService.AccountSyncController.&lt;Post&gt;d__4.MoveNext() in C:\Users\richa\Documents\Code\Company\Product\SubProduct\AccountSyncController.cs:line 32
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
     at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
     at System.Threading.ThreadPoolWorkQueue.Dispatch()
  </code></pre>