title: In-memory SQLite database isn't cleared after ASP .NET Core integration tests
body: <p>I'm writing integrations tests for an ASP .NET Core WebApi, using a WebApplicationFactory that will replace the DbContext by an SQLite in memory one :</p>
  <pre class="lang-cs prettyprint-override"><code>public class CustomWebApplicationFactory : WebApplicationFactory&lt;Startup&gt;
      {
          public ApplicationDbContext Context { get; private set; }
          private readonly string connectionString = &quot;DataSource=:memory:&quot;;
          private readonly SqliteConnection connection;
  
          public CustomWebApplicationFactory()
          {
              connection = new SqliteConnection(connectionString);
              connection.Open();
          }
  
          protected override void ConfigureWebHost(IWebHostBuilder builder)
          {
              Environment.SetEnvironmentVariable(&quot;JWT_SECRET&quot;,&quot;L*$w56jr!XZQZ$CFNQ7w$bM&amp;@oDQccNX4Um4RpKhtMQLhzRWM^fo$MGgoEPoSCrmonNn6pBqePLC&amp;kTyb6@KcWd3U%zRftyVzQvJSMZW*QAg3!kHh&amp;gD8zR7w6yGrS64&quot;);
              builder.UseEnvironment(&quot;Development&quot;);
              builder.ConfigureServices(services =&gt;
              {
                  var descriptor = services.SingleOrDefault(
                      d =&gt; d.ServiceType ==
                           typeof(DbContextOptions&lt;ApplicationDbContext&gt;));
  
                  services.Remove(descriptor);
                  // Create a new service provider.
                  var serviceProvider = new ServiceCollection()
                      .AddEntityFrameworkSqlite()
                      .BuildServiceProvider();
  
                  // Add a database context (AppDbContext) using an in-memory database for testing.
                  services.AddDbContext&lt;ApplicationDbContext&gt;(options =&gt;
                  {
                      options.UseSqlite(connection);
                      options.UseInternalServiceProvider(serviceProvider);
                  });
  
                  // Build the service provider.
                  var sp = services.BuildServiceProvider();
  
                  // Create a scope to obtain a reference to the database contexts
                  var scope = sp.CreateScope();
                  var scopedServices = scope.ServiceProvider;
                  var appDb = scopedServices.GetRequiredService&lt;ApplicationDbContext&gt;();
                  Context = appDb;
              });
          }
  
          protected override void Dispose(bool disposing)
          {
              base.Dispose(disposing);
              this.connection.Close();
          }
      }
  </code></pre>
  <p>And in my test class constructor, which I expect to be called for each test case, I do the following to empty the database between each test case :</p>
  <pre class="lang-cs prettyprint-override"><code>        public IntegrationTests(CustomWebApplicationFactory factory)
          {
              this.client = factory.CreateClient();
              this.db = factory.Context;
              factory.Context.Database.EnsureDeleted();
              factory.Context.Database.EnsureCreated();
          }
  
  
  
  </code></pre>
  <p>However, the data seems to be persistent between test cases. What would be the proper way to deal with this problem ?</p>
  <p>Thank you in advance,</p>