Pergunta

I'm trying to use Entity Framework with a SQLite database file, but can't make it work.

I'm using Visual Studio 2012, SQLite 1.0.92, and Entity Framework 6.1.

The edmx diagram correctly displays my tables, so I guess the mapping works, but when I try to do :

_Context = new DataContext();
var test = _Context.Set<T>().ToList();

I get the following exception :

No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SQLite'. 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.

As suggested in other SO posts, I tried adding this in my DbContext constructor to make sure the dll is copied :

var ensureDLLIsCopied = System.Data.Entity.SqlServer.SqlProviderServices.Instance;

The result is that instead of throwing and exception, my program just hangs forever.

I aslo tried to change the app.config file as suggested here and in this post's accepted answer, but no changes.

Any ideas?

Thank you


Edit :

I changed my DbContext from this :

public class DataContext : DbContext { }

to this :

public class DataContext : DbContext 
{ 
    public DataContext() : base("name=Entities") { }
}

Now, the exception I get is :

Cannot find 'Entities' connection string in the application configuration file

But the 'Entities' connection string is in app.config :

<add name="Entities" 
 connectionString="metadata=res://*/Entities.Model.csdl|res://*/Entities.Model.ssdl|res://*/Entities.Model.msl;provider=System.Data.SQLite;provider connection string=&quot;data source=mypath\dbname.db&quot;"
 providerName="System.Data.EntityClient" />
Foi útil?

Solução

Are you sure you have your app.config setup properly? I'm using EF6.1 with System.Data.SQLite v1.0.92.0 using the following app.config:

<?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>
  <system.data>
    <DbProviderFactories>
      <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" />
      <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" />
    </DbProviderFactories>
  </system.data>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>
</configuration>

This will need to be in the app.config for the executing program not the app.config for the assembly which contains your DbContext. Alternatively you could use code based configuration as I answered here which you can place in the same assembly as your DbContext.

And also yes if your DbContext is in a different assembly then that assembly will need some code referencing SQLite otherwise the compiler won't include the references to the required SQLite assemblies. You've provided an incorrect reference above to SqlServer rather than SQLite which won't work.

Outras dicas

if you used System.Data.SQLite v1.0.92.0

Replace the old snippet in Web.config:

  <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.SQLiteProviderServices, System.Data.SQLite.Linq"/>
  <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>

Instead of:

  <entityFramework>
<providers>
  <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>
</providers>

It works for me. The difference is in "invariantName", remove the "EF6".

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top