jeudi 28 mai 2015

SQLite: Entity Framework 6 - Wrong DbProviderFactory

I'm going bonkers with this.

I have a standard C# console application, with EF 6.1.3 and SQLite 1.0.97.0. I've set up my app.config to connect to the SQLite database using standard ADO, which works:

static void StandardConnection()
{
    try
    {
        SQLiteConnection db = new SQLiteConnection(ConfigurationManager.ConnectionStrings["StudioContext"].ConnectionString);
        db.Open();

        Console.WriteLine("Connected");
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

But for the love of me, I can't get EF to connect to this database. I have tried every single solution I could read up on, but every time I try to connect, I find the DBProviderFactory always points to {System.Data.SqlClient.SqlClientFactory}. It is driving me insane.

enter image description here

Following, my DbContext class:

public class SqliteContext: DbContext
{
    public SqliteContext()
        : base(ConfigurationManager.ConnectionStrings["StudioContext"].ConnectionString)
    {
        Database.SetInitializer<SqliteContext>(null);
    }
}

I don't want code first enabled, so disabled it here as this is off an existing application, being ported from PHP to C# and we have to work with the database as it is currently.

The code that's trying to connect:

static void EntityFrameworkConnection()
{
    try
    {
        using (var db = new SqliteContext())
        {
            if (db.Database.Connection.State == System.Data.ConnectionState.Closed)
            {
                db.Database.Connection.Open();
            }
        }
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

Very basic, open an instance of the context, check the state and connect if closed, this step is not required as I understand that EF will open the connection, if closed, when doing something like a save, for example, I'm just trying to get the DB to connect.

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>
  <connectionStrings>
    <add name="StudioContext" connectionString="Data Source=.\studio.db" providerName="System.Data.SQLite" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.97.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
      <provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6, Version=1.0.97.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </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, Version=1.0.97.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
      <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, Version=1.0.97.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" />
    </DbProviderFactories>
  </system.data>

Aucun commentaire:

Enregistrer un commentaire