Assemblies not being found - c#

The Entity Framework provider type 'Npgsql.NpgsqlServices, Npgsql.EntityFramework6.Npgsql' registered in the application config file for the ADO.NET provider with invariant name 'Npgsql' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information
Using Microsoft Visual Studio 2017 Community Edition
Using Entity Framework 6.2 (Installed via Nuget)
Using Npgsql 4.0.4 (Installed via Nuget)
Using VSIX for npsql installed via Microsoft Marketplace
Attempting code-first EF6 / postgres.
Just about tried every combination of app.config that I have seen examples on various posts. I am obviously missing something. Please help.
<?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>
<system.data>
<DbProviderFactories>
<remove invariant="Npgsql"/>
<add name="Npgsql Data Provider" invariant="Npgsql" description=".Net Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7"/>
</DbProviderFactories>
</system.data> </system.data>
<connectionStrings>
<add name="CricketConnection" connectionString="server=localhost;Database=CricketGrounds;Username=postgres;Password=postgres"
providerName="Npgsql" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework6.Npgsql" />
</providers>
</entityFramework>
</configuration>

I was getting this because I hadn't installed EntityFramework6.Npgsql which (among other things) adds stuff to App.Config
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Npgsql" publicKeyToken="5d8b90d52f46fda7" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.4.0" newVersion="4.0.4.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
I still have problems but now with System.Data.SqlClient.SqlClientFactory but if I can't solve that I'll post another question.

Related

Two different database access in .Net Framework 4.5.2 and Entity Framework 6

As mentioned, I have to configure two database which are MSSQL and MYSQL. For MSSQL case, it works but now I have to add new database (MySQL) but face this question below
The type 'MySql.Data.MySqlClient.MySqlConfiguration' does not inherit from 'System.Data.Entity.DbConfiguration'.
Entity Framework code-based configuration classes must inherit from 'System.Data.Entity.DbConfiguration'.
For 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>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="MySql.Data" publicKeyToken="c5687fc88969c44d" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-8.0.26.0" newVersion="8.0.26.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<entityFramework>
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient"></remove>
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=8.0.26.0" />
</DbProviderFactories>
</system.data>
</configuration>
Based on this XML:
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6" />
It appears that you may have installed the MySql.Data.Entity.EF6 NuGet package. That package is no longer supported and is not compatible with MySql.Data 8.0.26.
Uninstall it, and install MySql.Data.EntityFramework instead.
(Or if you installed MySql.Data.Entity, then note that it is also not compatible, as per my answer here. Again, uninstall and replace with MySql.Data.EntityFramework.)

How can I connect to a Oracle Database using Entity Framework and Oracle Managed Data Access?

I want to connect to a Oracle database using Entity Framework version 6.1.3. I'm working with Oracle Managed Data Acces package for Nuget in it's version 19.5.0 and Oracle Managed Data Access Entity Framework Nuget package in it's 19.3.0. Also I'm using Visual Studio 2019.
I want to create entities from the database tables using ADO .NET option in VS, but when I try to use this option and click in the Code First from database option, it doesn't show me any connection to oracle database.
I have my App.config file like this:
<?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"/>
<section name="oracle.manageddataaccess.client"
type="OracleInternal.Common.ODPMSectionHandler, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v13.0"/>
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
<provider invariantName="Oracle.ManagedDataAccess.Client"
type="Oracle.ManagedDataAccess.EntityFramework.EFOracleProviderServices, Oracle.ManagedDataAccess.EntityFramework, Version=6.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="Oracle.ManagedDataAccess.Client"/>
<add name="ODP.NET, Managed Driver" invariant="Oracle.ManagedDataAccess.Client" description="Oracle Data Provider for .NET, Managed Driver"
type="Oracle.ManagedDataAccess.Client.OracleClientFactory, Oracle.ManagedDataAccess, Version=4.122.19.1, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<publisherPolicy apply="no"/>
<assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral"/>
<bindingRedirect oldVersion="4.121.0.0 - 4.65535.65535.65535" newVersion="4.122.19.1"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
<oracle.manageddataaccess.client>
<version number="*">
<dataSources>
<dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) "/>
</dataSources>
</version>
</oracle.manageddataaccess.client>
<connectionStrings>
<add name="OracleDbContext" providerName="Oracle.ManagedDataAccess.Client"
connectionString="User Id=user;Password=password;Data Source=oracle"/>
</connectionStrings>
</configuration>
I don't know if the problem comes from this file or from another part. Also I have tried creating a new Data Connection, but when I select the Oracle Database option, it says that the provider is deprecated and I have to install Oracle Developer Tools for Visual Studio. I also download this and nothing changes. How can I solve this?
The solution I take to solve this is to uninstall VS 2019 and start to use VS 2017. This is because the ODAC drivers are only compatible with VS 2017 and older versions. Once I do this I was given the option to create a database using ODP .NET driver in the section of Server Explorer located in View -> Server Explorer -> Righ Click in Data Connections -> Add Connection
EDIT
Now there are the ODAC drivers to work with Visual Studio 2019

EntityFramework 6 won't work with MySql

I am trying to use my model and create a database from it. When i try to generate a database based on the model i get the following error:
Running transformation: System.InvalidOperationException: The SSDL generated by the activity called 'CsdlToSsdlAndMslActivity' is not valid and has the following errors:
No Entity Framework provider found for the ADO.NET provider with invariant name 'MySql.Data.MySqlClient'. 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.
at Microsoft.Data.Entity.Design.DatabaseGeneration.EdmExtension.CreateAndValidateStoreItemCollection(String ssdl, Version targetFrameworkVersion, IDbDependencyResolver resolver, Boolean catchThrowNamingConflicts)
at Microsoft.VisualStudio.TextTemplatingFED2D73CEED00C99E8A0A14FA9AE33EAFFDF3CAAFEE9E04D21CC913F1C5C88D960592C81A433C11663E61FD5459CF679CFD9B7275B72CA8BFB8D753DBB9FDDE3.GeneratedTextTransformation.get_Store() in C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\GenerateTSQL.Utility.ttinclude:line 57
at Microsoft.VisualStudio.TextTemplatingFED2D73CEED00C99E8A0A14FA9AE33EAFFDF3CAAFEE9E04D21CC913F1C5C88D960592C81A433C11663E61FD5459CF679CFD9B7275B72CA8BFB8D753DBB9FDDE3.GeneratedTextTransformation.TransformText() in C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\DBGen\SSDLToSQL10.tt:line 84 C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\Extensions\Microsoft\Entity Framework Tools\Templates\Includes\GenerateTSQL.Utility.ttinclude 57
The following contains my App.config if that is of any help:
<?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" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<connectionStrings>
<add name="SomeDatabase" providerName="MySql.Data.MySqlClient" connectionString="password=user;server=192.168.0.114;uid=hello;database=example;persistsecurityinfo=True;" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<entityFramework codeConfigurationType="MySql.Data.Entity.MySqlEFConfiguration, MySql.Data.Entity.EF6">
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.9.9.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"></assemblyBinding>
</runtime>
</configuration>
I know the issue is with the MySql.Data.MySqlClientbased on the error message, but i can't seem to figure out what it wants me to do. I have added MySql.Data, MySql.Data.Entity.EF6 and EntityFramework to the project.
Anyone knows how to fix this issue?
I was facing the same problem before this. I manage to solve it by creating a whole new project.
Go to Manage NuGet Packages, install EntityFramework, MySql.ConnectorNET.Data and MySql.ConnectorNET.Entity.
Use the connection string as shown here <add name="SomeDatabase" providerName="MySql.Data.MySqlClient" connectionString="server=localhost;port=3306;database=example;uid=hello;password=user"/>
Add [DbConfigurationType(typeof(MySql.Data.Entity.MySqlEFConfiguration))] in DbContext class.

How to correctly arrange WPF project that uses SQLite database with Entity Framework, and data access layer is in separate dll

I have prepared WPF app which connects to SQLite database.
My application is not organized into layered and I wanted to separate DAL - data access layer into separated project.
I have a problem with connection to database.
Should connection be defined in DAL dll app.config or in WPF app app.config?
Where should I store my sqlite database file?
Could you give good practice example?
I think such connection should be defined outside DLL but in such case I have a problem with defining that connection.
Entity Framework will look in the App.config of your main project for the SQLite connection string, so you'll want to put it there. Personally, I think the code should be independent of the connection string, since you may have different database copies for different needs.
If you have tests (which you should), they will probably point to a different test database than the actual application. The databases have the same structure, and the code should all behave the same, they just have different connection strings.
I personally prefer to keep all the config in one file; but, ultimately, it really depends on how you see the DAL dll being used. Think what might change and how the modules might get used.
Ex:
If you have multiple applications and you want them all share the same database, maybe putting it in the DAL config would make the most sense as if/when the connection string changes, you only have to change it in one place.
If this is to be a interface used by multiple different databases, the app config would make more sense as you don't want updates to DAL to push on top of the application's choice.
I have the same exact configuration: Entity Framework model with a DAL in a separate DLL. What I did was I created an app.config file in the DAL DLL project. This app.config file has all of the stuff that is needed for EF to use the SQLite provider, but no connection string.
Here is the app.config for the DAL DLL:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<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.97.0" newVersion="1.0.97.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
<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" />
</DbProviderFactories>
</system.data>
<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>
<providers>
<provider invariantName="System.Data.SQLite.EF6" type="System.Data.SQLite.EF6.SQLiteProviderServices, System.Data.SQLite.EF6" />
</providers>
</entityFramework>
</configuration>
I think this is all needed in the DLL for it to be able to be built.
The main program also has an app.config and references the DAL DLL project. Its app.config defines the connection string for accessing the database (which was created by the Entity Framework model wizard when I created the model). It also has all the other stuff and other things that are needed in my application.
Here's what that app.config looks like, with all of my app's specific stuff removed:
<?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" />
<!-- More stuff here for my app in particular . . . -->
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<connectionStrings>
<remove name="SQLiteConnection" />
<remove name="EntitiesConnection" />
<add name="SQLiteConnection" connectionString=". . ." />
<add name="EntitiesConnection" connectionString=". . ." />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0" />
</startup>
<system.data>
<!--
NOTE: The extra "remove" element below is to prevent the design-time
support components within EF6 from selecting the legacy ADO.NET
provider for SQLite (i.e. the one without any EF6 support). It
appears to only consider the first ADO.NET provider in the list
within the resulting "app.config" or "web.config" file.
-->
<DbProviderFactories>
<remove invariant="System.Data.SQLite" />
<add invariant="System.Data.SQLite" name="SQLite Data Provider" 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>
<!-- Ohter stuff particular to my app -->
<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.97.0" newVersion="1.0.97.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
The main program makes no database calls. Every database access is done through the code in the DAL dll.

SQLCE 4 Private Deployment Entity Framework 6

I am trying to privately deploy SQL CE WPF with Entity Framework 6 using Code First.
On Development machine database generates fine in AppData folder and all works fine however on Testing PC I get an exception (no db):
"The ADO.NET provide with invariant name 'System.Data.SQlServerCe.40' is either not registered in the machine or application config file'
I've tried to implement all available literature regarding this issue but still has a problem. I've checked the Bin folder with Deployed Folder and it is the same, all libraries (x86 and amd64) are present and Project Platform target is x86.
No Entity Framework provider found for the ADO.NET provider with invariant name 'System.Data.SqlServerCe.4.0'
http://erikej.blogspot.dk/2013/11/entity-framework-6-sql-server-compact-4_25.html
Entity Framewok Code First "ADO.NET provider not found" with local SQL Server CE DLL's
Bellow is 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" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<contexts>
<context type="MyProject.DAL.GreatContext, MyProject">
<databaseInitializer type="MyProject.Module.Database.Dal.GreatDbInitializer, MyProject" />
</context>
</contexts>
<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>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
<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>
I also have added the bellow code but still issue remains :
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.1.0.0" newVersion="4.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Any thoughts?
Assuming you use version 4,0,0,1 of the ado.net provider, you need to use this assembly redirect:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlServerCe" publicKeyToken="89845dcd8080cc91" culture="neutral"/>
<bindingRedirect oldVersion="4.0.0.0-4.0.0.1" newVersion="4.0.0.1"/>
</dependentAssembly>
</assemblyBinding>
Redirecting Entityframework.dll will not work.
And the proper solution is to use my EntityFramework.SqlServerCompact.PrivateDeployment NuGet package

Categories