Using EF6 SQLite with a plugin

59 views Asked by At

I'm implementing an SQLite Database for a plugin using EF6. However, one of my users gets the error "The default DbConfiguration instance was used before the 'SQLiteConfiguration' type was discovered"

Based on this post, I attributed my context with the SQLiteConfiguration as shown below, but that did not help. I know that this particular user is also using other plugins for his main software and I highly suspect that this has something to do with it. The EF6 docs specifically mention the case of a plugin database here, but I have no idea 1) whether I should implement MyProviderServices and MyConnectionFactory myself or whether the System.Data.SQLite library provides those and 2) how that would change my SQLiteConfiguration constructor, i.e. whether to keep the SetProviderFactory and SetProviderServices lines in there.

[DbConfigurationType(typeof(SQLiteConfiguration))]
public class Context : DbContext
{
    public Context() : base(
        new SQLiteConnection { ConnectionString = "some_connection_string"}, true)
    {
    }

    public DbSet<Foo> Foos{ get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        var sqliteConnectionInitializer = new SqliteDropCreateDatabaseWhenModelChanges<Context>(modelBuilder);
        System.Data.Entity.Database.SetInitializer(sqliteConnectionInitializer);
    }


public class SQLiteConfiguration : DbConfiguration
{
    public SQLiteConfiguration()
    {
        SetProviderFactory("System.Data.SQLite", SQLiteFactory.Instance);
        SetProviderFactory("System.Data.SQLite.EF6", SQLiteProviderFactory.Instance);
        SetProviderServices("System.Data.SQLite",
            (DbProviderServices)SQLiteProviderFactory.Instance.GetService(typeof(DbProviderServices)));
    }
}

Here is my app.config in case it helps. It was entirely autogenerated

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework"
                 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
                 requirePermission="false"/>
    </configSections>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Memory" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral"/>
                <bindingRedirect oldVersion="0.0.0.0-4.0.1.1" newVersion="4.0.1.1"/>
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Runtime.CompilerServices.Unsafe" publicKeyToken="b03f5f7f11d50a3a"
                                  culture="neutral"/>
                <bindingRedirect oldVersion="0.0.0.0-4.0.4.1" newVersion="4.0.4.1"/>
            </dependentAssembly>
        </assemblyBinding>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Data.SQLite"
                                  culture="neutral"
                                  publicKeyToken="db937bc2d44ff139"
                />
                <bindingRedirect oldVersion="0.0.0.0-1.0.117.0" newVersion="1.0.117.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
    <entityFramework>
        <providers>
            <provider invariantName="System.Data.SqlClient"
                      type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
        </providers>
    </entityFramework>
    <system.data>
        <DbProviderFactories>
            <remove invariant="System.Data.SQLite.EF6"/>
            <add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6"
                 description=".NET Framework Data Provider for SQLite (Entity Framework 6)"
                 type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6"/>
            <remove invariant="System.Data.SQLite"/>
            <add name="SQLite Data Provider" invariant="System.Data.SQLite"
                 description=".NET Framework Data Provider for SQLite"
                 type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite"/>
        </DbProviderFactories>
    </system.data>
</configuration>

Any help would be highly appreciated!

0

There are 0 answers