NpgSQL 3 Code First Connection String - entity-framework

I am trying to setup a project with EF6 and NpgSQL 3.x
I took the following steps:
Used Nuget to install Entity Framework 6
List item
Add connections string to .config file using syntax described here: http://www.npgsql.org/doc/connection-string-parameters.html
Used Nuget to install NpgSQL EntityFramework6 package
When I try to connect to the database I get the error:
"Keyword not supported: 'port'."
referring to the port setting in the connection string
if i remove the "port" the connection fails with error:
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
The error message seems like it is trying to connect using SQL server protocol not postgres. Is there any additional steps I need to take to get this setup?
Most of the .config file examples in the documentation appear outdated so I am wondering if need to manually add a setting to the config somewhere.
If anyone has this setup working could you share your .config files thanks.

You're right about : "it is trying to connect using SQL server protocol not postgres", because it's the default provider.
You have to add the Npgsql provider to the DbProviderFactories and EF providers in web.config, then link the connection string to that provider (see providerName), so entityframe can connect to postgresql:
<system.data>
<DbProviderFactories>
<add name="Npgsql Data Provider" invariant="Npgsql" description="Data Provider for PostgreSQL" type="Npgsql.NpgsqlFactory, Npgsql" />
</DbProviderFactories>
</system.data>
<entityFramework>
<providers>
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
</entityFramework>
<connectionStrings>
<add name="CS" connectionString="Server=127.0.0.1;Port=5432;Database=MYDB;User Id=postgres;Password=mypass" providerName="Npgsql" />
</connectionStrings>

Related

provider with invariant name 'Devart.Data.PostgreSql' is either not registered in the machine or application config file, or could not be loaded

I get this error message when using my API with PostGres database.
"The ADO.NET provider with invariant name 'Devart.Data.PostgreSql' is either not registered in the machine or application config file, or could not be loaded. See the inner exception for details."
I have the feeling I need to install Devart product on my staging and production server to fix the error. Is it right? In case this is right I would like to understand why simply copying or publishing Devart dll is not enough? Normally it should be enough.
In error message I can read "is either not registered". What does it mean to be registered? Is it rehgistered in confi file or I need to do a specific operation on my staging and produciton machine?
You should register configuration information in the DbProviderFactories section of the *.config file:
<system.data>
<DbProviderFactories>
<remove invariant="Devart.Data.PostgreSql" />
<add name="dotConnect for PostgreSQL" invariant="Devart.Data.PostgreSql" description="Devart dotConnect for PostgreSQL" type="Devart.Data.PostgreSql.PgSqlProviderFactory, Devart.Data.PostgreSql, Version=7.12.1328.0, Culture=neutral, PublicKeyToken=09af7300eec23701" />
</DbProviderFactories>
</system.data>
Replace 7.12.1328.0 here with your actual version.
Refer to https://www.devart.com/dotconnect/postgresql/docs/?deployment.html.

Trying to use DDtek Datadirect Sybase drivers as a provider for Enttiy Framework 6, can't get it configured correctly

Trying to use DDtek Datadirect Sybase drivers as a provider for Enttiy Framework 6, can't get it configured correctly..
It looks like in 3.3 there is no DDtek.Sybase.Entity,
We have a trial version of 4.2 which has the DLL, so I've installed that on.
Here is my providers section of the web.config, where I think the error lives
<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="DDTek.Sybase.4.2" type="DDTek.Sybase.Entity.ProviderServices, DDTek.Sybase.Entity,
Version=4.2.0.0, Culture=neutral, PublicKeyToken=c84cd5c63851e072" /
</providers>
</entityFramework>
and here is my connection string
<add name="DefaultConnection" connectionString="host='XXXX.XXX';Pooling=true;Port='6000';UID='XXXXX';Password=XXXXX;Database='XXXX';Min Pool Size=5;Load Balance Timeout=30;Connection Timeout = 30000;Max Pool Size=50;Workstation ID='SPN'; Clone Connection If Needed =true; Fetch Buffer Size = 40960" providerName="DDTek.Sybase.4.2" />
The connection string, minus the providername bit works in 3.3, non EF.
Here is the error I'm getting...
The Entity Framework provider type
'DDTek.Sybase.Entity.ProviderServices, DDTek.Sybase.Entity,
Version=4.2.0.0, Culture=neutral, PublicKeyToken=c84cd5c63851e072'
registered in the application config file for the ADO.NET provider
with invariant name 'DDTek.Sybase.4.2' 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.
Looking at more documentation I changed the name to DDTek.Sybase (default) instead of DDTek.Sybase.4.2 (recommended fro multiple versions installed (which there are, but only one supports EF)
and this is the error I get instead
The Entity Framework provider type
'DDTek.Sybase.Entity.ProviderServices, DDTek.Sybase.Entity,
Version=4.2.0.0, Culture=neutral, PublicKeyToken=c84cd5c63851e072'
registered in the application config file for the ADO.NET provider
with invariant name 'DDTek.Sybase' 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.
Has anyone successfully set this up using the Progress/DataDirect drivers?
I'm 100% noob to Entity Framework, so no idea is too small, or unappreicated
The DataDirect Drivers do not support EF 6.0 as they previously told me, it
only supports 5.0.
So I converted it to work with MSDB, in like 10 min (Most of which was commenting out bits of Sybase + ddtek stuff)
The new connectionstring looked like this
<add name="DefaultConnectionMSold" connectionString="Data Source=XXXXXXXXX;Initial Catalog=my_project;Integrated Security=True" providerName="System.Data.SqlClient"/>

Entity Framework connection work locally but not on remote server after deploy

I have created a web application in ASP.NET MVC using a SQL Server database, and I used Entity Framework with a database-first approach.
This is my connection string for Entity Framework
<add name="AcModelFinalContext"
connectionString="metadata=res://*/Models.Maha-Earth-Model.csdl|res://*/Models.Maha-Earth-Model.ssdl|res://*/Models.Maha-Earth-Model.msl;provider=System.Data.SqlClient;provider connection string="data source=DESKTOP-IE0U7PA\SQLEXPRESS;initial catalog=AC-Services;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
This connection string is working fine on localhost.
For deployment I made made changes in string as
<add name="AcModelFinalContext"
connectionString="metadata=res://*/Models.Maha-Earth-Model.csdl|res://*/Models.Maha-Earth-Model.ssdl|res://*/Models.Maha-Earth-Model.msl;provider=System.Data.SqlClient;provider connection string="data source=dburl.com\SQLEXPRESS;initial catalog=AC-Services;integrated security=True;User Id=mydb;Password=mypass;MultipleActiveResultSets=True;App=EntityFramework""
providerName="System.Data.EntityClient" />
but this does not work.
Try to remove integrated security=True.
When you use this option, User/Password is not used.
You should also try to remove \SQLEXPRESS. I don't think your provider is using Sql-Server Express.

Entity framework gives error underlying provider failed on open

I developed a web site on my local pc and local database using entity framework, Now I gave all the code to my client now the entity framework gives an error underlying provider failed on open I modified data source, initial catalog, user id and password of the connection string of entity framework but of no use please help. below is my connection string.
<add name="SBV_dbEntities" connectionString="metadata=res://*/SBVEntityModel.csdl|res://*/SBVEntityModel.ssdl|res://*/SBVEntityModel.msl;provider=System.Data.SqlClient;provider connection string="data source=.\SQLEXPRESS;initial catalog=yagnesh_db;user id=yagnesh;password=yagnesh#123;multipleactiveresultsets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
use like below.Give a connection string to the SQL server is as below (This is a sample code).
<add name="ContextName" connectionString="Data Source=ServerName;Initial Catalog=DBCatalogName;Integrated Security=True;MultipleActiveResultSets=True;App=EntityFramework"
providerName="System.Data.SqlClient" />
e.g.
<add name="PawLoyalty" connectionString="Server=.;database=PawLoyalty;Trusted_connection=true;pooling=true;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
I hope this will help to you.

SQL Server Express connection string for Entity Framework Code First

I am working in Visual Web Developer 2010 Express, and using the Entity Framework code-first CTP. I am able to do this with the new SQL Server CE but I am unable to find a connection string to work with SQL Server Express.
This one, using the SQL Server CE beta, works fine (database is created and recreated on model changes).
<add name="TrempimModel"
connectionString="data source=|DataDirectory|TrempimModel.sdf"
providerName="System.Data.SqlServerCe.4.0" />
This one, which I copied from the aspnetdb connections string,
<add name="TrempimModel"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|TrempimModel.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
Gives the following message on startup:
Unable to complete operation. The supplied SqlConnection does not specify an initial catalog
So how to make it work with SQL Server Express?
The problem with your connection string here is:
<add name="TrempimModel"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|aspnetdb.sdf;
User Instance=true"
providerName="System.Data.SqlClient" />
You're basically defining what "server" you're connecting to - but you're not saying what database inside the file to connect to. Also - the file extension for SQL Server Express database files is .mdf (not .sdf - that's SQL Server Compact Edition) - you need to take that into account, too! (was a typo, according to comment by OP).
You need to define an extra database=.... (or Initial Catalog=.....) in your connection string:
<add name="TrempimModel"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;
database=YourDatabaseName;
AttachDBFilename=|DataDirectory|aspnetdb.mdf;
User Instance=true"
providerName="System.Data.SqlClient" />
Then it should work just fine.
For more background and tons of samples, check out the ConnectionStrings web site.