Tom Lindley
Tom Lindley

Reputation: 371

SQLite Entity Framework ADO.NET Error

I am trying to implement a data service based on EF (6.1.3) and SQLite. I have it working in a very small test app, but can't see to duplicate this experience. My data service class library has the system.data.sqlite.... components loaded. I also loaded SQLite.CodeFirst as I read that the create functions in EF 6.1.3 are not complete for SQLite. However, the error I get when the data service is called is:

System.InvalidOperationException occurred HResult=0x80131509 Message=No Entity Framework
provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'.
Make sure the provider is registered in the 'entityFramework' section of the application config file. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information.

I think this must in the app.config file, but can't seem to find out whats wrong with the configuration.

Here is the code for the context class.

    public class IRMContext : DbContext
{
    public DbSet<Operator> Operators { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        var sqlLiteInit = new SqliteDropCreateDatabaseWhenModelChanges<IRMContext>(modelBuilder);
        Database.SetInitializer<IRMContext>(sqlLiteInit);
    }
}

And the app.config file.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
        <section name="entityFramework" 
 type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, 
 EntityFramework, Version=6.0.0.0, Culture=neutral, 
 PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
 </configSections>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
  </startup>
  <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
          <assemblyIdentity name="System.Data.SQLite" publicKeyToken="db937bc2d44ff139" culture="neutral"/>
    <bindingRedirect oldVersion="0.0.0.0-1.0.96.0" newVersion="1.0.96.0"/>
  </dependentAssembly>
</assemblyBinding>
</runtime>
<connectionStrings>
    <add name="IRMContext" connectionString="Data Source=C:\IRManager\IRManager.sqlite" providerName="System.Data.SQLite"/>
</connectionStrings>
    <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
  <parameters>
    <parameter value="v13.0"/>
  </parameters>
    </defaultConnectionFactory>
    <providers>
       <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>
       <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/pointers would be greatly appreciated. Thanks

Upvotes: 0

Views: 741

Answers (1)

Tom Lindley
Tom Lindley

Reputation: 371

I traced my problem down to two issues.

  1. The config string needed to be in the actual application app.config file, not in the app.config for the class library.
  2. The application needs to have SQLite installed as it appears to want to load SQLite from the app not the library.

My main issue seems to be most of my experience with SQLite has been in UWP applications where this is not the case.

Thanks.

Upvotes: 2

Related Questions