Sqlite and EntityFramework 6 Invalid cast on SqlConnection to EntityConnection

entity-framework-6 sqlite

Question

I'm facing a InvalidCastException (SqliteConnection to EntityConnection) and is driving me crazy because I don't find much information about it and the one I find simply doesn't work.

I guess is something wrong with the app.config file but I've try a lot of things without any result.

Also try changing the DbContext but with the same result, nothing.


Here the app.config file

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework"/>
    <providers>
      <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" />
     </providers>
    </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" />
         <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>
  <connectionStrings>
    <add name="MailServerDBConnectionString"
         connectionString="metadata=res://*/Database.DatabaseEntities.MailServerDBModel.csdl|res://*/Database.DatabaseEntities.MailServerDBModel.ssdl|res://*/Database.DatabaseEntities.MailServerDBModel.msl;provider=System.Data.SQLite.EF6;provider connection string=&quot;data source=C:\MailServerDB.s3db&quot;"
         providerName="System.Data.SQLite" />
  </connectionStrings>

Here the DbContext:

public MailServerDBConnectionString()
        : base("name=MailServerDBConnectionString")
    {
    }

Here how I use it:

try
{
            using (MailServerDBConnectionString context = new MailServerDBConnectionString())
            {
                    var inbound = context.C_DIRECTION.Find("INBOUND");
            }
}
catch (Exception crap)
{
            throw new Exceptions.DBException("Still not f*cking working", crap);
}

I've try everything and I hope someone has face this problem before, any suggestions is always welcome, thanks for your time.

1
0
11/27/2015 3:01:01 PM

Accepted Answer

After a couple of hours and differents errors I just find the solution (or at least make it work for the moment)

"Just" change the providerName of the ConnectionString from System.Data.SQLite to System.Data.EntityClient on the app.config file.

Hope this works for the next guy who face similar problems.

1
11/27/2015 3:02:05 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow