title: EntityFrameworkCore SQLite in-memory db tables are not created
body: <p>For integration tests I am using an <code>EntityFrameworkCore</code> <code>SQLite</code> in-memory db and creating its schema as per Microsoft docs, but when I attempt to seed data an exception is thrown that tables do not exist.</p>
  
  <p>The mouse-over docs for <code>DbContext.Database.EnsureCreated();</code> :</p>
  
  <blockquote>
    <p>Ensure that the database for the context exists.  If it exists, no
    action is taken.  If it does not exist then the database and all its
    schema are created.  If the database exists, then no action is made to
    ensure it is compatible with the model for this context.</p>
  </blockquote>
  
  <p>I've read that an <code>EntityFrameworkCore</code> in-memory db only exists as long as an open connection exists, and so I tried explicitly creating a <code>var connection = new SqliteConnection("DataSource=:memory:");</code> instance and wrapping the below code in a <code>using(connection) {}</code> block and passing the connection instance <code>options.UseSqlite(connection);</code>, but <code>DbContext.Database.EnsureCreated();</code> still doesn't create any db-objects</p>
  
  <pre><code>public class CustomWebApplicationFactory&lt;TStartup&gt; : WebApplicationFactory&lt;Startup&gt;
  {
      protected override IWebHostBuilder CreateWebHostBuilder()
      {
          return WebHost.CreateDefaultBuilder()
              .UseStartup&lt;Startup&gt;();
      }
  
      protected override void ConfigureWebHost(IWebHostBuilder builder)
      {
        using (var connection = new SqliteConnection("DataSource=MySharedInMemoryDb;mode=memory;cache=shared"))
        {
            connection.Open();
            builder.ConfigureServices(services =&gt;
            {
                var serviceProvider = new ServiceCollection()
                    .AddEntityFrameworkSqlite()
                    .BuildServiceProvider();
  
                services.AddDbContext&lt;MyDbContext&gt;(options =&gt;
                {
                    options.UseSqlite(connection);
                    options.UseInternalServiceProvider(serviceProvider);
                });
  
                var contextServiceProvider = services.BuildServiceProvider();
  
                // we need a scope to obtain a reference to the database contexts
                using (var scope = contextServiceProvider.CreateScope())
                {
                    var scopedProvider = scope.ServiceProvider;
  
                    var logger = scopedProvider.GetRequiredService&lt;ILogger&lt;CustomWebApplicationFactory&lt;TStartup&gt;&gt;&gt;();
  
                    using (var myDb = scopedProvider.GetRequiredService&lt;MyDbContext&gt;())
                    {
                        // DEBUG CODE
                        // this returns script to create db objects as expected
                        // proving that MyDbContext is setup correctly
                        var script = myDb.Database.GenerateCreateScript();
                        // DEBUG CODE
  
                        // this does not create the db objects ( tables etc )
                        // this is not as expected and contrary to ms docs
                        var result = myDb.Database.EnsureCreated();
  
                        try
                        {
                            SeedData.PopulateTestData(myDb);
                        }
                        catch (Exception e)
                        {
                            // exception is thrown that tables don't exist
                            logger.LogError(e, $"SeedData.PopulateTestData(myDb) threw exception=[{e.Message}]");
                        }
                    }
                }
            });
          }
          builder.UseContentRoot(".");
          base.ConfigureWebHost(builder);
      }
  </code></pre>
  
  <p>Please note that in this post I'm only asking the question why doesn't <code>DbContext.Database.EnsureCreated();</code> create the schema as expected. I'm not presenting the above code as a general pattern for running integration-tests.</p>