I would like if anyone knows how to use Npgsql with .NET 5.0. Under .net Framework (4.8) it is very handy and no problem. But under .NET 5.0 or 6.0, i can not.
Update : Sorry. Here's the problème.
When i try to use the NpgSQL package i face the following exception :
{"The specified invariant name 'Npgsql' wasn't found in the list of registered .NET Data Providers."}
- App.config file (The exact same file works under .NET Framework 4.8) : Failed
<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" support="FF" />
</DbProviderFactories>
</system.data>
<entityFramework>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, EntityFramework6.Npgsql" />
</providers>
<defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql" />
</entityFramework>
- Directly in the code : Failed as well
public MainWindow()
{
InitializeComponent();
try
{
using (DbConnection connection = CreateDbConnection("Npgsql", "host = localhost; port = 5432; database = MyDatabas; user id = MyUser; password = MyPassword"))
{
if (connection != null)
{
connection.Open();
connection.Close();
label1.Content = "CreateDbConnection Ok";
}
else
label1.Content = "CreateDbConnection Failed";
}
return;
}
catch (Exception ex)
{
label1.Content = ex.Message;
return;
}
}
static DbConnection CreateDbConnection(
string providerName, string connectionString)
{
// Assume failure.
DbConnection connection = null;
// Create the DbProviderFactory and DbConnection.
if (connectionString != null)
{
try
{
DbProviderFactory factory =
DbProviderFactories.GetFactory(providerName);
connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
}
catch (Exception ex)
{
// Set the connection to null if it was created.
if (connection != null)
{
connection = null;
}
Console.WriteLine(ex.Message);
}
}
// Return the connection.
return connection;
}
Thank you for help.
CodePudding user response:
.NET 5 (and .NET Core) don't use App.config any more. While you can still access App.config via System.Configuration, DbProviderFactories is no longer automatically populated from it.
If you're just looking to use Npgsql, you can directly instantiate NpgsqlConnection in your code (new NpgsqlConnection()
instead of factory.CreateConnection()
). If you're looking to use DbProviderFactories in order to support multiple drivers, you need to pre-register Npgsql's DbProviderFactory at program start, as follows:
DbProviderFactories.RegisterFactory("Npgsql", NpgsqlFactory.Instance);
CodePudding user response:
Ok,
The problem was an extension of my previous post WPF : Use NpgSQL Entity Framework
Now with Shay Rojansky's help, it works fine.
- Configure the App.config file like previous post (link above).
- Add the line below to register the provider before using it.
DbProviderFactories.RegisterFactory("Npgsql", NpgsqlFactory.Instance);