Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to determine the provider name for provider factory of type 'Npgsql.NpgsqlFactory'

I trying running a test project but I not have a success because have a fail with npgsql connection. I reinstalled Npgsql, Npgsql.EntityFramework, EntityFramwork... but the problem persist. Result Message:

Método de teste TestUserControl.ControleBaseTest.TesteGetPermissoesUsuarioFixo gerou exceção:

System.NotSupportedException: Unable to determine the provider name for provider factory of type 'Npgsql.NpgsqlFactory'. Make sure that the ADO.NET provider is installed or registered in the application config. Result StackTrace:

I have this 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>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <providers>
      <provider invariantName="System.Data.SqlClient"     type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework">    </provider>
    </providers>
    <!--<defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql" />-->
    <defaultConnectionFactory type="Npgsql.NpgsqlConnectionFactory, Npgsql" />
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Npgsql" />
      <add name="Npgsql Data Provider" invariant="Npgsql" support="FF" description=".Net Framework Data Provider for Postgresql" type="Npgsql.NpgsqlFactory, Npgsql" />
    </DbProviderFactories>
  </system.data>
</configuration>

And a DbConfig is...

public class CtxUser : DbContext
{
    public CtxUser(string conexao) : base(conexao) { }
    public CtxUser(DbConnection conexao) : base(conexao, true) { }
}

My Model...

[Serializable]
[Table("mytable", Schema = "public")]
public class Users
{
    [Key, Column("userid")]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    [Column("login")]
    public string Login { get; set; }

    [Column("password")] 
    public string Password { get; set; }
}

My logical class...

private DbConnection Conexao { get; set; }
public ControleBase(DbConnection connector)
{
    if(conexao == null)
        throw new ArgumentException("blah blah", "connector");
    Conexao = conexao;
}

public Usuarios GetUser(int id)
{
    if(id <= 0)
        throw new ArgumentException("blah blah");
    Users user;
    using (var ctx  = new CtxUser(Connection))
    {
        user = ctx.DbUser.Find(id);
    }
    return user;
}

My test method...

[TestClass]
public class ControleBaseTest
{
    private DbConnection Conexao
    {
        get
        {
            var cnxBuilder = new NpgsqlConnectionStringBuilder
            {
                Database = "mydatabase",
                Host = "localhost",
                Port = 5432,
                UserName = "postgres",
                Password = "postgres",
                Enlist = true,
                CommandTimeout = 30,
                Timeout = 30,
                Pooling = true,
                MinPoolSize = 1,
                MaxPoolSize = 20,
                Compatible = new Version(2, 1),
                PreloadReader = true
            };
            return new NpgsqlConnection(cnxBuilder);
        }
    }
    
    [TestMethod]
    public void TestGetUser()
    {
        IUserControl userControl = new ControleBase(Conexao);            
        var usuario = userControl.GetUsuario(9);
        Assert.IsNotNull(usuario);
    }
}

Obvious, this test is only for test connection with Npgsql and linq, etc... for test a solution for the problem. Someone have a idea?

like image 675
GustavoAdolfo Avatar asked Oct 31 '22 16:10

GustavoAdolfo


1 Answers

I had the same problem and fixed it by setting the Npgsql.dll to be copied locally in the reference properties. For some reason VS 2013 (maybe other versios as well) turns this to "false" at seemingly random points in time. This results in a build where it doesn't copy the .dll to your compilation directory resulting in the error described above.

I hope it helps you, but it may be something different for you.

like image 162
Laurentiu Ilici Avatar answered Nov 15 '22 06:11

Laurentiu Ilici