title: How to setup SQLite in WebApplicationFactory?
body: <p>I am writing an integration test for my ASP.NET Core MVC application. The test is about to send a <code>POST</code> request to controller and then check if database was updated correctly.</p>
  <p>I have a <code>CustomWebApplicationFactory</code> where I am trying to configure SQLite in-memory database, but probably I do something wrong.</p>
  <pre><code>public class CustomWebApplicationFactory&lt;TStartup&gt; : WebApplicationFactory&lt;TStartup&gt; where TStartup : class
  {
      private SqliteConnection Connection;
  
      protected override void ConfigureWebHost(IWebHostBuilder builder)
      {
          Connection = new SqliteConnection(&quot;DataSource=:memory:&quot;);
          Connection.Open();
  
          builder.UseEnvironment(&quot;Development&quot;);
  
          builder.ConfigureTestServices(services =&gt;
          {
              // Unregister existing database service (SQL Server).
              var descriptor = services.SingleOrDefault(
                  d =&gt; d.ServiceType ==
                      typeof(DbContextOptions&lt;AppDbContext&gt;));
  
              if (descriptor != null) services.Remove(descriptor);
  
              // Register new database service (SQLite In-Memory)
              services.AddDbContext&lt;AppDbContext&gt;(options =&gt; options.UseSqlite(Connection));
          });
      }
  
      protected override void Dispose(bool disposing)
      {
          base.Dispose(disposing);
          Connection.Close();
      }
  }
  </code></pre>
  <p>My test looks like this:</p>
  <pre><code>public class OrderControllerTests : IClassFixture&lt;CustomWebApplicationFactory&lt;Startup&gt;&gt;
  {
      private readonly HttpClient _httpClient;
      private readonly AppDbContext _context;
  
      public OrderControllerTests(CustomWebApplicationFactory&lt;Startup&gt; factory)
      {
          _httpClient = factory.CreateDefaultClient();
  
          var scopeFactory = factory.Services.GetService&lt;IServiceScopeFactory&gt;();
  
          using var scope = scopeFactory.CreateScope();
  
          _context = scope.ServiceProvider.GetService&lt;AppDbContext&gt;();
      }
  
      [Fact]
      public async Task Create_Post_OrderIsCreated()
      {
          // ...
          _context.Customers.Add(customer);
          _context.SaveChanges();
         // ...
      }
  }
  </code></pre>
  <p>When I run the test, the line <code>_context.Customers.Add(customer);</code> triggers the <code>CustomWebApplicationFactory.Dispose()</code> method and I get an error:</p>
  <blockquote>
  <p>System.ObjectDisposedException : Cannot access a disposed context instance. A common cause of this error is disposing a context instance 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 instance, or wrapping it in a using statement. If you are using dependency injection, you should let the dependency injection container take care of disposing context instances.</p>
  <p>Object name: 'AppDbContext'.</p>
  </blockquote>
  <p>The error message is very descriptive, but I don't know how to solve it. Why is the database context getting disposed?</p>