title: Running EF Core with SQLite in memory - No Database Provider has been configured
body: <p>I am writing an ASP.Net Core Web API using EF Core with SQL server and StructureMap for IOC. 
  The library projects are all <code>.NetStandard2.0</code> while the API and Unit Tests are <code>NetCoreApp2.0</code>.</p>
  
  <p>For the unit tests I'm running XUnit and swapping out SQL Server for an in memory SQLite db as it provides full referential integrity. 
  I'm still using the same IOC setup as my main code but I pass in a list of Structure map registries to allow me to substitute in a SQLite context factory rather than the sql one.</p>
  
  <p>This is a trimmed down version of my DbContext:</p>
  
  <pre><code>public class MyDbContext : DbContext
  {
      public MyDbContext(DbContextOptions options) : base(options)
      {
      }
  }
  </code></pre>
  
  <p>and this is my test setup code:</p>
  
  <pre><code>public IServiceProvider ServiceProvider { get; set; }
  
  public MyServiceTests()
  {
      var services = new ServiceCollection();
      var dataRegistry = new Registry();
  
      dataRegistry.For&lt;IContextFactory&lt;MyDbContext&gt;&gt;().Use&lt;SqlLiteContextFactory&gt;();
  
      if (SqlLiteContextFactory.Connection == null)
      {
          SqlLiteContextFactory.Connection = new SqliteConnection("DataSource=:memory:");
          SqlLiteContextFactory.Connection.Open();
      }
  
      services
          .AddEntityFrameworkSqlite()
          //This is only here as some posts suggested this was needed, StartUp.cs for production site does not have this and works fine.
          .AddDbContext&lt;MyDbContext&gt;(options =&gt; options.UseSqlite(SqlLiteContextFactory.Connection));
  
      var registries = new List&lt;Registry&gt;
      {
          dataRegistry,
          new CommandQueryRegistry(),
          new ServiceRegistry(),
          new TransformRegistry()
      };
  
      ServiceProvider = DependencyInjection.TestSetup(services, registries);
  }
  </code></pre>
  
  <p>The IOC initialisation code is fairly basic:</p>
  
  <pre><code>public static IServiceProvider TestSetup(IServiceCollection services, List&lt;Registry&gt; registries)
  {
      var container = new Container();
  
      var registry = new Registry();
      registries.ForEach(r =&gt; registry.IncludeRegistry(r));
  
      container.Configure(config =&gt;
      {
          config.AddRegistry(registry);
          config.ForSingletonOf&lt;IHttpContextAccessor&gt;().Use&lt;HttpContextAccessor&gt;();
          config.Populate(services);
      });
  
      var serviceProvider = container.GetInstance&lt;IServiceProvider&gt;();
      return serviceProvider;
  }
  </code></pre>
  
  <p>This is the context factory code for my SQLite context factory, the only difference between this and the SQL one is that I have the static <code>Connection</code> property to ensure I don't lose the db once the context is disposed.</p>
  
  <pre><code>public class SqlLiteContextFactory : IContextFactory&lt;MyDbContext&gt;
  {
      public static SqliteConnection Connection;
  
      private DbContextOptions&lt;MyDbContext&gt; CreateOptions(bool trackChanges)
      {
          var builder = new DbContextOptionsBuilder&lt;MyDbContext&gt;();
          var optionsBuilder = new DbContextOptionsBuilder&lt;MyDbContext&gt;();
          optionsBuilder.UseSqlite(Connection);
  
          if (!trackChanges)
          {
              builder.UseQueryTrackingBehavior(QueryTrackingBehavior.NoTracking);
          }
  
          return builder.Options;
      }
  
      private MyDbContext CreateDbContext(bool trackChanges)
      {
          if (Connection == null)
          {
              Connection = new SqliteConnection("DataSource=:memory:");
              Connection.Open();
          }            
  
          var context = new MyDbContext(CreateOptions(trackChanges));
  
          //Always keep context as most recent version in tests
          context.Database.Migrate();
  
          return context;
      }
  
      public MyDbContext CreateNonTrackedContext()
      {
          return CreateDbContext(false);
      }
  
      public MyDbContext CreateDbContext()
      {
          return CreateDbContext(true);
      }
  }
  </code></pre>
  
  <h1>The Problem</h1>
  
  <p>My code runs fine when running the site, the SQL context factory creates the context and runs the migrate command to create the database no problem. 
  However, when I attempt to test any of my services through the unit tests, the context factory blows up when trying to run <code>Migrate</code> with the following:</p>
  
  <pre><code>System.InvalidOperationException : No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions&lt;TContext&gt; object in its constructor and passes it to the base constructor for DbContext.
     at Microsoft.EntityFrameworkCore.Internal.DbContextServices.Initialize(IServiceProvider scopedProvider, IDbContextOptions contextOptions, DbContext context)
     at Microsoft.EntityFrameworkCore.DbContext.get_InternalServiceProvider()
     at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.GetRelationalService[TService](IInfrastructure`1 databaseFacade)
     at Microsoft.EntityFrameworkCore.RelationalDatabaseFacadeExtensions.Migrate(DatabaseFacade databaseFacade)
     at API.Test.Utilities.SqlLiteContextFactory.CreateDbContext(Boolean trackChanges) in API.Test\Utilities\SqLiteContextFactory.cs:line 37
     at API.Test.Utilities.SqlLiteContextFactory.CreateDbContext() in API.Test\Utilities\SqLiteContextFactory.cs:line 49
     at API.CommandQueries.Commands.MyCommand.AddOrUpdate(MyModel model) in \API.CommandQueries\Commands\MyCommand.cs:line 21
     at API.Services.MyService.Save(Model model) in API.Services\MyService.cs:line 40
     at API.Test.MyTests.CanAdd() in API.Test\MyServiceTests.cs:line 47
  </code></pre>
  
  <p>I have tried every solution I can find to this problem. Adding in the <code>.AddDbContext</code> to the service collection. Making sure that both the test project and the context project have references to <code>EntityFrameworkCore</code>, <code>EntityFrameworkCore.Relational</code> and <code>EntityFrameworkCore.Sqlite</code>. Ensuring that the SQLite connection is kept alive and making sure the test set up uses <code>.AddEntityFrameworkSqlite()</code> on the <code>ServiceCollection</code>.</p>
  
  <p>I've also tried swapping SQLite out for EF Cores <code>InMemory</code> Db with yet another context factory but that fails with exactly the same problem.</p>
  
  <p>Has anyone else seen this issue before or am I using EF Core in some way that makes it incompatible with SQLite somehow?</p>