c# – 无法确定“Npgsql.NpgsqlFactory”类型的提供程序工厂的提供程序名称

我尝试运行测试项目,但我没有成功,因为npgsql连接失败.我重新安装了Npgsql,Npgsql.EntityFramework,EntityFramwork ……但问题仍然存在.

结果消息:

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:

我有这个app.config文件:

<?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>

DbConfig就是……

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

我的模特……

[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; }
}

我的逻辑课……

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;
}

我的测试方法……

[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);
    }
}

显而易见,此测试仅用于与Npgsql和linq等的测试连接…用于测试问题的解决方案.有人有想法吗?

最佳答案 我有同样的问题,并通过设置Npgsql.dll在本地复制修复它

参考属性.出于某种原因,VS 2013(也许还有其他版本)会改变这种情况

在看似随机的时间点上“假”.这导致构建不成功

将.dll复制到编译目录,导致上述错误.

我希望它可以帮助你,但它可能会有所不同.

点赞