title: Npgsql with .NET 5.0
body: <p>I would like if anyone knows how to use <strong>Npgsql</strong> with <strong>.NET 5.0</strong>. Under <strong>.net Framework</strong> (4.8) it is very handy and no problem. But under .NET 5.0 or 6.0, i can not.</p>
  <p><strong>Update</strong> :
  Sorry. Here's the problème.</p>
  <p>When i try to use the NpgSQL package i face the following exception :</p>
  <blockquote>
  <p>{&quot;The specified invariant name 'Npgsql' wasn't found in the list of
  registered .NET Data Providers.&quot;}</p>
  </blockquote>
  <ul>
  <li>App.config file (The exact same file works under .NET Framework 4.8) : Failed</li>
  </ul>
  <pre><code>  &lt;system.data&gt;
      &lt;DbProviderFactories&gt;
        &lt;remove invariant=&quot;Npgsql&quot; /&gt;
        &lt;add name=&quot;Npgsql Data Provider&quot; invariant=&quot;Npgsql&quot; description=&quot;.Net Data Provider for PostgreSQL&quot; type=&quot;Npgsql.NpgsqlFactory, Npgsql, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7&quot; support=&quot;FF&quot; /&gt;
      &lt;/DbProviderFactories&gt;
    &lt;/system.data&gt;
    
    &lt;entityFramework&gt;
      &lt;providers&gt;
        &lt;provider invariantName=&quot;System.Data.SqlClient&quot; type=&quot;System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer&quot; /&gt;
        &lt;provider invariantName=&quot;Npgsql&quot; type=&quot;Npgsql.NpgsqlServices, EntityFramework6.Npgsql&quot; /&gt;
      &lt;/providers&gt;
      &lt;defaultConnectionFactory type=&quot;Npgsql.NpgsqlConnectionFactory, EntityFramework6.Npgsql&quot; /&gt;
    &lt;/entityFramework&gt;
  </code></pre>
  <ul>
  <li>Directly in the code : Failed as well</li>
  </ul>
  <pre><code>        public MainWindow()
          {
              InitializeComponent();
  
  
              try
              {
                  using (DbConnection connection = CreateDbConnection(&quot;Npgsql&quot;, &quot;host = localhost; port = 5432; database = MyDatabas; user id = MyUser; password = MyPassword&quot;))
                  {
                      if (connection != null)
                      {
                          connection.Open();
                          connection.Close();
                          label1.Content = &quot;CreateDbConnection Ok&quot;;
                      }
                      else
                          label1.Content = &quot;CreateDbConnection Failed&quot;;
                  }
                  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;
          }
  </code></pre>
  <p>Thank you for help.</p>