How to integrate PostgreSql with EntityFramework 6.0.2? [duplicate] - c#

This question already has an answer here:
Entity Framework 6 with Npgsql
(1 answer)
Closed 3 months ago.
I am receiving following error
The 'Instance' member of the Entity Framework provider type
'Npgsql.NpgsqlServices, Npgsql, Version=2.0.14.2, Culture=neutral,
PublicKeyToken=5d8b90d52f46fda7' did not return an object that
inherits from 'System.Data.Entity.Core.Common.DbProviderServices'.
Entity Framework providers must inherit from this class and the
'Instance' member must return the singleton instance of the provider.
This may be because the provider does not support Entity Framework 6
or later; see http://go.microsoft.com/fwlink/?LinkId=260882 for more
information.
Can any one help me
here is my 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>
<connectionStrings>
<add name="CacsLocal" connectionString="Server=localhost;Database=Kappa;User Id=postgres; Password=xyz;" providerName="Npgsql" />
<add name=" CacsCentral" connectionString="Server=localhost;Database=KPT1;User Id=postgres; Password=insane1234;" providerName="Npgsql" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql" />
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"></provider>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
I have Npgsql2 version 2.0.14.2
Entity Framework 6.0.2
All the packages are installed through Package Manager by following command
Install-Package Npgsql -Pre
Install-Package EntityFramework -Pre
Thanks

In App.config file insert this code befoere the <system.data>
<!--PostgreSQL Connection-->
<connectionStrings>
<add name="PGConnectionString" connectionString="Host=localhost;DataBase=codefirst;User Id=postgres;Password=Sanju0212#;" providerName="Npgsql" />
</connectionStrings>
run few commands on package manager console
1.Enable-Migration
2. Add-Migration
3. Update-database -verbose

Related

C# WPF EF6 LOCALDB crashs on access database

I've deployed a wpf simple application with localdb mdf embeded and entity framework 6, when I test my application at windows 7 the application crash when try access database.
I already installed localdb version 2012, 2014 and netframework 4.7 in this client machine but keeps crashing on query window.
I'm using Visual Studio 2015 Anyone have experiencied this?
dependencies installed in client machine:
net framework. 4.6.1 or above
LocalDB 32BIT\SqlLocalDB.msi or LocalDB 64BIT\SqlLocalDB.msi
Here my appconfig:
<?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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
<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>
<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>
<connectionStrings>
<add name="AgroTarifaEntities" connectionString="metadata=res://*/ModelAgro.csdl|res://*/ModelAgro.ssdl|res://*/ModelAgro.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\AgroTarifa.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
<add name="AgroTarifa.Properties.Settings.AgroTarifaConnectionString"
connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\AgroTarifa.mdf;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
</configuration>
This is just a hunch, but maybe that connection string is being used as a literal. Try replacing all instances of " with a single quote. Let me know if that works-it may come up in my work down the road.
I Fixed the problem installing on windows 7 client ssdt package it's not the best practice... but fixed for the user.
here the SSDT resource page
https://learn.microsoft.com/pt-br/sql/ssdt/download-sql-server-data-tools-ssdt?view=sql-server-2017

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.

Upgraded EF5 to EF6 - getting No Entity Framework provider found

I have updated my project from EF5 to EF6, using NuGet, but am getting an error now as soon as my application tried to query the database.
error 0152: 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 the issue is with my data project's App.Config, which resembles 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" />
</configSections>
<connectionStrings>
<add name="AccuFinanceEntities" connectionString="metadata=res://*/AccuFinanceEntities.csdl|res://*/AccuFinanceEntities.ssdl|res://*/AccuFinanceEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=laptop;initial catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True;App=AccuFinance"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
My web.config only has a connection string applied:
<connectionStrings>
<!--SERVER -->
<!--LOCAL-->
<add name="AccuFinanceEntities" connectionString="metadata=res://*/AccuFinanceEntities.csdl|res://*/AccuFinanceEntities.ssdl|res://*/AccuFinanceEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=laptop;initial catalog=mydb;Integrated Security=True;MultipleActiveResultSets=True;App=AccuFinance"" providerName="System.Data.EntityClient" />
</connectionStrings>
I think I need to add or remove something from one of these, but not sure which. It worked fine under EF5. Project compiles and builds, and I can edit my EDMX file (I can update model from database).
What am I doing wrong?

Entity Framework in Windows Service

I have solution with 3 projects:
1) - library
2) - test (windows forms application)
3) - windows service
Library uses Entity Framework. When i link it to test application it works. My App.config (for test application) looks like this:
<?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="ITSBPEntities1" connectionString="metadata=res://*/DBModel.csdl|res://*/DBModel.ssdl|res://*/DBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=192.168.0.131;initial catalog=ITSBP;persist security info=True;user id=user;password=pass;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
I've referenced to library in service also. I installed it and run, but when it try to use Entity Framework some exception is thrown, there is log:
The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' 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.
App.config in service project is copy-pasted from test application. EntityFramework.dll exists in debug folder.

How do I tell Entity Framework to use SQL Server Compact in code first?

How do I change my app.config or Migration Configuration.cs file so Entity Framework uses a local SDF file instead of a local SQL Server instance ? I am using EF 6.1.
Thanks
<?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.SqlConnectionFactory, EntityFramework" />
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
You should just be able to specify the ProviderName in the connection string to your database.
<connectionStrings>
<add name="DataModelContext"
providerName="System.Data.SqlServerCe.4.0"
connectionString="Data Source=location\cfdb.sdf"/>

Categories