how to use EF6 with SQLite? - entity-framework

I have a project, I use nuget to install SQLite 1.0.93.0. This install EF6 and add the references to the project. Also update my app.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<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>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, 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" />
</providers>
</entityFramework>
</configuration>
Then I create a new connection to my sqlite database and I try to create an edmx, but I get an error that says that my project has a reference to the newest version of EF but there is no a compatible provider for this connection to the database. If I have a a compatible provider I have to recompile the project, or if I have not a compatible provider I have to install it and recompile the project.
I try to recompile the project but the problem persists. I don't know how to install a compatible provider.
If in the references of the project I delete the references to EntityFramework.dll and EntityFramework.SQLserver.dll then in the wizard to create the edmx a have the option to choose between EF6.x and EF5. only have enabled the option to select EF5, so in this case I use EF5 and not EF6.
How can I use EF6 with SQLite?
Thanks.

Please see if my answer here works for you (be sure to watch the video) : Database first create entity framework 6.1.1 model using system.data.sqlite 1.0.93

Related

SQLite Entity Framework ADO.NET Error

I am trying to implement a data service based on EF (6.1.3) and SQLite. I have it working in a very small test app, but can't see to duplicate this experience. My data service class library has the system.data.sqlite.... components loaded. I also loaded SQLite.CodeFirst as I read that the create functions in EF 6.1.3 are not complete for SQLite.
However, the error I get when the data service is called is:
System.InvalidOperationException occurred HResult=0x80131509 Message=No Entity Framework
provider found for the ADO.NET provider with invariant name 'System.Data.SqlClient'.
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.
I think this must in the app.config file, but can't seem to find out whats wrong with the configuration.
Here is the code for the context class.
public class IRMContext : DbContext
{
public DbSet<Operator> Operators { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
var sqlLiteInit = new SqliteDropCreateDatabaseWhenModelChanges<IRMContext>(modelBuilder);
Database.SetInitializer<IRMContext>(sqlLiteInit);
}
}
And the app.config file.
<?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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Data.SQLite" publicKeyToken="db937bc2d44ff139" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-1.0.96.0" newVersion="1.0.96.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<connectionStrings>
<add name="IRMContext" connectionString="Data Source=C:\IRManager\IRManager.sqlite" providerName="System.Data.SQLite"/>
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SQLite" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6"/>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</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"/>
<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>
Any help/pointers would be greatly appreciated. Thanks
I traced my problem down to two issues.
The config string needed to be in the actual application app.config file, not in the app.config for the class library.
The application needs to have SQLite installed as it appears to want to load SQLite from the app not the library.
My main issue seems to be most of my experience with SQLite has been in UWP applications where this is not the case.
Thanks.

EF6 with Npgsql under Asp.net 5

Hi I am trying to use EF6 with npsql under Asp.net 5. I am using a simple console (asp.net5) for testing and a separate class lib (.net 4.5) for the data access as a simple test enviroment. Have installed EF6 using
nuget EntityFramework6.Npgsql. Have also removed .net core dependencies from project.json file.
The problem i keep on getting the following error.
An exception of type 'System.NotSupportedException' occurred in EntityFramework.dll but was not handled in user code
Additional information: Unable to determine the provider name for provider factory of type 'Npgsql.NpgsqlFactory'. Make sure that the ADO.NET provider is installed or registered in the application config.
It seems to have trouble with the provider name for 'Npgsql.NpgsqlFactory'. But the provider name in the configuration is set. May be it needs to be set when it was compiled. Any ideas?
the app.config file is as follows
<?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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<providers>
<provider invariantName="Npgsql.NpgsqlFactory" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework"></provider>
</providers>
<defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql" />
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql" />
<add name="Npgsql" invariant="Npgsql" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
</configuration>
Any help will be much appreciated. Many thanks.
You are missing the complete assembly information for npgsql in your configuration. Please, check this answer for more information about it and the complete information:
https://stackoverflow.com/a/35440114/246097
I hope it helps.

Entity Framework looks for ADO.NET provider SqlServerCe.3.5 instead of 4.0

I am using SQL Server CE 4.0 along with entity framework 6.0. Everythings work fine until I wanted to enable a N-Tier syncing mechanism with a remote database using a WCF service and the Sync Framework 2.1.
I followed this tutorial and use the JTune's workaround to overcome the issue that the SyncFramework 2.1 and SQLSErverCompactCE 4.0 compatibility is not supported. Which is simply a runtime binding redirect in app.config for System.Data.SqlServerCE.dll 4.0.0
Then I get the following App.config file
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<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.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="System.Data.SqlServerCe.4.0" type="System.Data.Entity.SqlServerCompact.SqlCeProviderServices, EntityFramework.SqlServerCompact" />
</providers>
</entityFramework>
<connectionStrings>
<add name="Entities" connectionString="metadata=res://*/Persistence.Model1.csdl|res://*/Persistence.Model1.ssdl|res://*/Persistence.Model1.msl;provider=System.Data.SqlServerCe.4.0;provider connection string="data source=|DataDirectory|\XXXX.sdf"" providerName="System.Data.EntityClient" />
</connectionStrings>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845DCD8080CC91" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.4.0" />
<add name="Microsoft SQL Server Compact Data Provider 4.0" invariant="System.Data.SqlServerCe.4.0" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91" />
</DbProviderFactories>
</system.data>
</configuration>
It works fine on some configuration but may fail with the following error.
No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlServerCe.3.5'. 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.
Remark that the solution provided in this question is not viable, because in my situation, I really need the binding redirect to use SyncFramework.
Is there a way to tell entity framework not to look for a SQL Server CE 3.5 provider and only for the 4.0?
I think I got this. When SQL Server CE 3.5 is installed on the machine then its added to the DbProviders in the machine.config and depending on its position regarding the 4.0 version you may have the failure (see my comment above). It looks like the app.config lets you the possibility to remove a DbProviderFactory. I added the following line in my app.config and the problem seems to be resolved
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SqlServerCe.3.5" />
</DbProviderFactories>
</system.data>

How to create edmx avoiding sqlite-netFx45-setup-bundle-x86-2012-1.0.84.0.exe installation dependency

I have designed an app using EF6 and Sqlite. I had already downloaded EF6 and Sqlite for Windows Runtime (System.Data.Sqlite) from NuGet package installations. But as still Sqlite was not coming in the Provider list to generate Entity Framework model from db using its configuration dialog, I installed sqlite-netFx45-setup-bundle-x86-2012-1.0.84.0.exe from Sqlite.org. With the installation, Sqlite provider is added in the Provider list and using that my edmx file has been generated and the SSDL generated like this
<edmx:StorageModels>
<Schema Namespace="MyModel.Store" Alias="Self" Provider="System.Data.SQLite" ProviderManifestToken="ISO8601" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="MyModelStoreContainer">..</EntityContainer>
<EntityType Name="XXX"></EntityType>
</Schema>
</edmx:StorageModels>
Other than this, every where in my project, I used all the assembly references localy(NuGet downloaded local assembly path) like this,
<Reference Include="EntityFramework">
<HintPath>.\packages\EntityFramework.6.0.2\lib\net40\EntityFramework.dll</HintPath>
</Reference>
<Reference Include="System.Data.SQLite">
<HintPath>.\packages\System.Data.SQLite.1.0.91.3\lib\net40\System.Data.SQLite.dll</HintPath>
</Reference>
<Reference Include="System.Data.SQLite.EF6">
<HintPath>.\packages\System.Data.SQLite.1.0.91.3\lib\net40\System.Data.SQLite.EF6.dll</HintPath>
</Reference>
after successful generation of my edmx and working properly with my application, I want to uninstall the sqlite-netFx45-setup-bundle from my pc to avoid any kind of dpendency(any assembly reference from GAC due to that installation) at deployment side.
But after the uninstallation, I get an error
Error 5 Error 175: The specified store provider cannot be found in the configuration, or is not valid. ..\EntityFramework\MyModel.edmx
It seems the error is pointing a missing GAC assembly reference for Sqlite provider at edmx storage model. My question is how can I remove the dependency of the that tool once my edmx is generated. Can it be resolved by adding any new assemly reference at the project or specific reference (how) to my existing local assembly (System.Data.Sqlite). Or any modification required at the app.config?
<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.SqlConnectionFactory, EntityFramework"/>
<providers>
<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>
<connectionStrings>
<add name="MyModelEntity" connectionString="metadata=res://*/EntityFramework.MyModel.csdl|res://*/EntityFramework.MyModel.ssdl|res://*/EntityFramework.MyModel.msl;provider=System.Data.SQLite;provider connection string="data source=.\Database\SqliteDB.db"" providerName="System.Data.EntityClient"/>
</connectionStrings>

Postgresql, Entity Framework and Mono 3

I'm trying to get EF to work with Postgresql on a Linux platform. When I try to connect to the database I end up with this error: Failed to find or load the registered .Net Framework Data Provider 'Npgsql Data Provider'. My app.config file:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework" />
</configSections>
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql"></remove>
<add name="Npgsql Data Provider"
invariant="Npgsql"
description=".Net Framework Data Provider for Postgresql Server"
type="Npgsql.NpgsqlFactory, Npgsql, Version=4.0.0.0, Culture=neutral" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="MinDatabase" connectionString="Server=localhost;Port=5432;Database=postgres;User Id=postgres;Password=;CommandTimeout=20;"
providerName="Npgsql Data Provider" />
</connectionStrings>
</configuration>
How can I make Mono load the data provider?
The error message contains the invariant name, so its looking for something with invariant name "Npgsql Data Provider" and you specifically set the invariantName to Npgsql. So maybe try to change to use the same name all over your configuration like this:
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql"></remove>
<add name="Npgsql"
invariant="Npgsql"
description=".Net Framework Data Provider for Postgresql Server"
type="Npgsql.NpgsqlFactory, Npgsql, Version=4.0.0.0, Culture=neutral" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="MinDatabase" connectionString="Server=localhost;Port=5432;Database=postgres;User Id=postgres;Password=;CommandTimeout=20;"
providerName="Npgsql" />
</connectionStrings>
Hope it works
Where did you download Npgsql from? If you are using Npgsql from official releases, your problem is the Version attribute of the assembly. Npgsql which supports EF 4.x has version 2.0.14.3 and Npgsql which supports EF 6 has version 2.1.0. None has version 4.0.0.0.
For more info about using Npgsql with Entity Framework, check out:
How to integrate PostgreSql with EntityFramework 6.0.2?
Entity Framework 6 with Npgsql
http://fxjr.blogspot.com.br/2014/02/using-entity-framework-6-with-npgsql-210.html
I hope it helps.