title: EF6, SQLite won't work without App.config
body: <p>I'm trying to make a plug in that will use EF6.1 and SQLite for an app where I can't change the App.config so all the configuration and connection string needs to be set in code.</p>
  
  <p>I found this answer that looked promising <a href="https://stackoverflow.com/questions/20460357/problems-using-entity-framework-6-and-sqlite?rq=1">Problems using Entity Framework 6 and SQLite</a></p>
  
  <p>So now I have a configuration class like this:</p>
  
  <pre><code>public class CollectionDbConfiguration : DbConfiguration
  {
      public CollectionDbConfiguration()
      {
          SetProviderServices("System.Data.SQLite"(DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
          SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
          SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);                 
      }
  }
  </code></pre>
  
  <p>I have confirmed this gets hit before the context is created for the first time and all these return values.</p>
  
  <p>My context looks like this</p>
  
  <pre><code>public class MyContext : DbContext
  {
      public MyContext(string connectionString)
      :   base(connectionString) { }
  
      public DbSet&lt;MyEntity&gt; MyEntities { get; set; }      
  }
  </code></pre>
  
  <p>And I have some code calling it like this:</p>
  
  <pre><code>var context = new MyContext("Data Source = mytest.db; Version = 3;");
  var entities = context.MyEntities.ToList();
  </code></pre>
  
  <p>When I try and run the code it looks like the context is assuming the connection string is for SQL Server as it gives me:</p>
  
  <p>Keyword not supported: 'version'.</p>
  
  <p>If I remove it I then get an error that it cannot connect and its clearly trying to connect to a SQL Server database.</p>
  
  <p>I tried passing in a SQLite connection by adding a constructor:</p>
  
  <pre><code>public MyContext(DbConnection connection) 
      : base(connection, contextOwnsConnection: true)
  { }
  </code></pre>
  
  <p>And calling it with this:</p>
  
  <pre><code>var context = new MyContext(new SQLiteConnection("Data Source = mytest.db; Version = 3;"));
  var entities = context.MyEntities.ToList();
  </code></pre>
  
  <p>But then I get the error:</p>
  
  <p>Unable to determine the DbProviderFactory type for connection of type 'System.Data.SQLite.SQLiteConnection'. Make sure that the ADO.NET provider is installed or registered in the application config.</p>
  
  <p>So I made a factory resolver and registered that</p>
  
  <pre><code>public class FactoryResolver : IDbProviderFactoryResolver
  {
      public DbProviderFactory ResolveProviderFactory(DbConnection connection)
      {
          if (connection.GetType() == typeof(SQLiteConnection))
          {
              return SQLiteFactory.Instance;
          }
  
          if (connection.GetType() == typeof(EntityConnection))
          {
              return SQLiteProviderFactory.Instance;
          }
  
          return null;
      }
  }
  </code></pre>
  
  <p>And added:</p>
  
  <pre><code>SetProviderFactoryResolver(new FactoryResolver());
  </code></pre>
  
  <p>but now I get this:</p>
  
  <p>No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite.EF6'. Make sure the provider is registered in the 'entityFramework' section of the application config file. See <a href="http://go.microsoft.com/fwlink/?LinkId=260882" rel="nofollow noreferrer">http://go.microsoft.com/fwlink/?LinkId=260882</a> for more information.</p>
  
  <p>I've beet at this for two days now and I'm running out of ideas.</p>