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

时间:2014-10-03 18:38:24

标签: c# asp.net-mvc postgresql unit-testing npgsql

我尝试运行一个测试项目但是没有成功,因为npgsql连接失败了。我重新安装了Npgsql,Npgsql.EntityFramework,EntityFramwork ......但问题仍然存在。 结果消息:

  

Métododeteste TestUserControl.ControleBaseTest.TesteGetPermissoesUsuarioFixogerouexditionção:

     

System.NotSupportedException:无法确定“Npgsql.NpgsqlFactory”类型的提供程序工厂的提供程序名称。确保在应用程序配置中安装或注册了ADO.NET提供程序。   结果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等的测试连接...用于测试问题的解决方案。有人有想法吗?

1 个答案:

答案 0 :(得分:2)

我遇到了同样的问题并通过将Npgsql.dll设置为本地复制来修复它 参考属性。出于某种原因,VS 2013(也许还有其他版本)会改变这种情况 在看似随机的时间点上“假”。这导致构建不成功 将.dll复制到编译目录,导致上述错误。

我希望它对你有帮助,但对你来说可能会有所不同。