title: Why is my context disposed before I can use it?
body: <p>I'm implementing a very simple query using the <code>HotChocolate</code> GraphQL library in a .Net 5 project. I've been following the <a href="https://github.com/ChilliCream/graphql-workshop/blob/master/docs/1-creating-a-graphql-server-project.md" rel="nofollow noreferrer">tutorial series</a> from the HotChocolate GitHub repository but in my project I don't want GraphQL accessing the context directly and would instead prefer for it to access a repository that manages database access via the context.</p>
  <p>I've built a few REST endpoints in this project alongside GraphQL and this repository pattern works properly there. However when I call these repository methods from a GraphQL, the context is disposed before the repository method uses it.</p>
  <p>I am guessing that the way HotChocolate uses the context causes it to get disposed earlier than I am expecting, but I am having trouble figuring out when/where it gets disposed and how I can prevent it from being disposed so my repository methods will work.</p>
  <p><strong>ContentRepository.cs</strong></p>
  <pre class="lang-c# prettyprint-override"><code>namespace BLL.Repository
  {
      public class ContentRepository : IRepository&lt;Content&gt;
      {
          private readonly CmsContext _dbContext;
  
          public ContentRepository(CmsContext dbContext)
          {
              _dbContext = dbContext;
          }
  
          public virtual List&lt;Content&gt; GetContent()
          {
              return _dbContext.Content.ToList();
          }
      }
  }
  </code></pre>
  <p><strong>Query.cs</strong></p>
  <pre class="lang-c# prettyprint-override"><code>namespace Web.GraphQL
  {
      public class Query
      {
          private readonly IContentRepository _contentRepository;
  
          public Query(IContentRepository contentRepository)
          {
              _contentRepository = contentRepository;
          }
          
          public List&lt;Content&gt; GetContent()
          {
              return _contentRepository.GetContent() as List&lt;Content&gt;;
          }
      }
  }
  </code></pre>
  <p><strong>Startup.cs</strong></p>
  <pre class="lang-c# prettyprint-override"><code>namespace Web
  {
      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }
  
          public IConfiguration Configuration { get; }
  
          public void ConfigureServices(IServiceCollection services)
          {
              services.AddControllersWithViews();
              services.AddControllers().AddNewtonsoftJson();
              
              services.AddDbContext&lt;CmsContext&gt;(options =&gt;
                  options.UseMySql(Configuration.GetConnectionString(&quot;DefaultConnection&quot;), ServerVersion.AutoDetect(Configuration.GetConnectionString(&quot;DefaultConnection&quot;))));
              
              services
                  .AddGraphQLServer()
                  .ModifyRequestOptions(options =&gt; options.IncludeExceptionDetails = true)
                  .AddQueryType&lt;Query&gt;();
  
              services.AddScoped&lt;IRepository&lt;Content&gt;, ContentRepository&gt;();
          }
  
          public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerFactory loggerFactory)
          {
              if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
  
              app.UseHttpsRedirection();
              app.UseRouting();
  
              app.UseEndpoints(endpoints =&gt;
              {
                  endpoints.MapControllerRoute(&quot;default&quot;, &quot;/{controller}/{action}&quot;,
                      new {controller = &quot;Default&quot;, action = &quot;Index&quot;});
              });
  
              app.UseEndpoints(endpoints =&gt; endpoints.MapGraphQL());
  
              app.UseGraphQLVoyager(new VoyagerOptions
              {
                  GraphQLEndPoint = &quot;/graphql&quot;
              }, &quot;/graphql-voyager&quot;);
          }
      }
  }
  </code></pre>
  <p>When debugging and stepping through the code, once it has reached the <code>GetContent</code> method in <code>ContentRepository.cs</code> it throws an <code>ObjectDisposedException</code>.</p>
  <p>What do I need to do to ensure that <code>CmsContext</code> is still available to the <code>ContentRepository</code> when called from a GraphQL query?</p>