vendredi 23 octobre 2015

Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'

I was using EF for MSSQL all my life and it works seamlessly but when I just tried to make it work with sqlite - no luck.

I tried to look all over the internet but cannot find answers. Tried everything from this thread and also this blog.

Basically all I want to do is create a Code-First database solution using sqlite. But I get this error. every time I try to add/save an object.

An unhandled exception of type 'System.NotSupportedException' occurred in EntityFramework.dll

Additional information: Unable to determine the provider name for provider factory of type 'System.Data.SqlClient.SqlClientFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.

Here is my 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>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v12.0" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <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>
    <connectionStrings>
      <add name="MainDataContext" connectionString="Data Source=|DataDirectory|gh.sqlite" providerName="System.Data.SQLite.EF6" />
    </connectionStrings>
    <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>

Here is the code

class Program
{
    static void Main(string[] args)
    {
        var db = new MainDataContext();
        var user = new User { Id = 1 };
        db.Users.Add(user); // Error
        db.SaveChanges();
    }
}

Here is the context

class MainDataContext : DbContext
{
    public DbSet<User> Users { get; set; }
}

It's a super small application and still something is wrong, presumably in the config. If someone had any luck doing this in sqlite please point me to the right direction.

Aucun commentaire:

Enregistrer un commentaire