'Why is Entity Framework Core using SqlConnection on Azure?

I'm creating an application using PostgreSQL database.

I'm using the AddDbContext.UseNpgsql() methods to work with it :

Startup.cs:

services.AddDbContext<DbModel>(options => 
    options.UseNpgsql(
        Configuration.GetConnectionString("connectionStringName"))

When I'm starting the application on my computer (Windows 11), it works. I'm able to connect to my PostgreSQL database, perform migrations, CRUD actions and else.

But when I'm trying to deploy it on an Azure App Service (running Linux) I get the following error :

Application startup exception System.ArgumentException: Keyword not supported: 'port'.

at Microsoft.Data.Common.DbConnectionOptions.ParseInternal(Dictionary2 parsetable, String connectionString, Boolean buildChain, Dictionary2 synonyms, Boolean firstKey)
at Microsoft.Data.Common.DbConnectionOptions..ctor(String connectionString, Dictionary`2 synonyms)
at Microsoft.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, DbConnectionOptions& userConnectionOptions)
at Microsoft.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
at Microsoft.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerConnection.CreateDbConnection()
...

Even if I'm using the UseNpgsql() method, it seems like on the App Service only, Entity Framework Core is still trying to use SQL Server instead of PostgreSQL, and it cannot recognize the keyword "port".

But why? How can I tell it to use the correct Npgsql provider?

UPDATE 1 I've changed my local connection string to make it throws an error, here is the stack trace I have in local when a keyword is not supported :

System.ArgumentException: Keyword not supported: azerty (Parameter 'keyword') at Npgsql.NpgsqlConnectionStringBuilder.GetProperty(String keyword) at Npgsql.NpgsqlConnectionStringBuilder.set_Item(String keyword, Object value) at System.Data.Common.DbConnectionStringBuilder.set_ConnectionString(String value) at Npgsql.NpgsqlConnectionStringBuilder..ctor(String connectionString) at Npgsql.NpgsqlConnection.GetPoolAndSettings()
at Npgsql.NpgsqlConnection.set_ConnectionString(String value) at Npgsql.NpgsqlConnection..ctor(String connectionString) at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlRelationalConnection.CreateDbConnection() at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.get_DbConnection()

In local, it uses the correct provider. Do I have to change a setting on the Azure App Service ?



Solution 1:[1]

I am converting this to an answer which may help other community members who are facing similar kind of issue.

So, after setting up the broken deployment pipeline, there is no other issue and the above problem got fixed. Now you can be able to connect to the PostgreSQL database successfully without any errors.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RajkumarMamidiChettu-MT