Unable to determine the provider name for provider factory of type "System.Data.Sqlite.SqliteFactory"

zhnglicho picture zhnglicho · Aug 3, 2015 · Viewed 19.5k times · Source

I want to use sqlite entity framework in my web api project, but it always can't work well,

here is my development enviroment.

1.Visual studio 2013, .net framework 4.5

  1. sqlite package version is 1.0.97, I installed below packages

    system.data.sqlite, system.data.sqlite.ef6, system.data.sqlite.linq

  2. EntityFramework is 6.1.3

Here is exception that I got

Unable to determine the provider name for provider factory of type 'System.Data.SQLite.SQLiteFactory'. Make sure that the ADO.NET provider is installed or registered in the application config

Here is my webconfig

I can connect sqlite database file through "connect to database" in tool of visual studio, I also can generate entity using code first.

but I can't get data normal

Here is my code

 public partial class Sqlite : DbContext
    {
        public Sqlite()
            : base("name=Sqlite")
        {
        }

        public virtual DbSet<AuthorizationLog> AuthorizationLogs { get; set; }
        public virtual DbSet<Checksum> Checksums { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<AuthorizationLog>()
                .Property(e => e.ClientKey)
                .IsUnicode(false);

            modelBuilder.Entity<AuthorizationLog>()
                .Property(e => e.Login)
                .IsUnicode(false);

            modelBuilder.Entity<AuthorizationLog>()
                .Property(e => e.Password)
                .IsUnicode(false);

            modelBuilder.Entity<AuthorizationLog>()
                .Property(e => e.ConnectionString)
                .IsUnicode(false);
        }
    }

   public class ValuesController : ApiController
    {
        // GET api/values
        Sqlite ctx = new WebApi2Demo.Sqlite();
        public IEnumerable<Checksum> Get()
        {
            return ctx.Checksums;
        }
	}

Answer

zhnglicho picture zhnglicho · Aug 4, 2015

I got the answer by myself, but I still don't know the root cause, now it works. I changed webconfig, here is the webconfig that make my project to work.

I added a provider that is "System.Data.Sqlite", Please note its type that is same with System.Data.Sqlite.EF6

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

  <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" />

Here is all configure.

 <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
    </providers>
  </entityFramework>

  <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>