数据库
首页 > 数据库> > 在Dapper 1.42中具有自定义类型处理程序的参数的查询上的Npgsql 3.0.0阻塞

在Dapper 1.42中具有自定义类型处理程序的参数的查询上的Npgsql 3.0.0阻塞

作者:互联网

此问题似乎与Npgsql 3.0.0中的某些内容(在Dapper 1.42下有所更改)有关.我得出了这个结论,因为以下代码在将Npgsql 2.2.3与相同版本的Dapper一起使用时可以正常工作:

public struct MyStruct
{
    private readonly int _val;
    public MyStruct(int i) { _val = i; }
    public int MyInt { get { return _val; } }
}



public class MyStructHandler : SqlMapper.TypeHandler<MyStruct>
{
    public override MyStruct Parse(object value)
    {
        if (value is int)
        {
            return new MyStruct((int)value);
        }
        throw new FormatException("Invalid Conversion to MyStruct");
    }

    public override void SetValue(IDbDataParameter parameter, MyStruct value)
    {
        parameter.DbType = DbType.Int32;
        parameter.Value  = value.MyInt;
    }
}



public static int Main()
{
    var myStructHandler = new MyStructHandler();
    SqlMapper.AddTypeHandler(myStructHandler);

    const string query = "SELECT * FROM MyTable WHERE ID = :someID";
    var queryParams = new DynamicParameters();
    queryParams.Add("someID", new MyStruct(3));
    IDbConnection connection = new NpgsqlConnection(connectionString);
    var result = connection.Execute(query, queryParams);
}

运行上面的代码时,出现以下消息异常:

Problem with query: SELECT * FROM MyTable WHERE ID = :someID
The given key was not present in the dictionary
System.Collections.Generic.KeyNotFoundException : The given key was not present in the dictionary.

我已验证以下内容:

1) Passing in parameters of built-in types works correctly. E.g., if I change the line queryParams.Add("someID", new MyStruct(3)); to queryParams.Add("someID", 3);, the query returns as expected.
2) MyStructHandler.Parse works correctly when deserializing a field of type MyStruct
3) The parameter name and casing matches between the query and the DynamicParams object
4) MyStructHandler.SetValue is NEVER called prior to throwing the exception

该错误似乎是新引入的,并且仅影响具有自定义Dapper处理程序的类.有谁知道为什么在我升级到Npgsql 3.0.0后,它不再起作用,更重要的是,如何解决它?

-其他信息-

该异常在Npgsql.NpgsqlParameter.set_DbType(DbType值)中引发,其中对System.Collections.Generic.Dictionary`2.get_Item(TKey)进行了一些调用.同样,这曾经在Npgsql 2.2.3中起作用,因此我假设该代码中发生了一些更改,从而打破了该用例.

解决方法:

此问题已被报告并解决,请参阅https://github.com/npgsql/npgsql/issues/694

您可以立即获取不稳定的nuget包来解决此问题(http://www.npgsql.org/install.html),或者再等几天,直到我们发布具有此修复程序的Npgsql 3.0.1.

标签:postgresql,dapper,npgsql,c
来源: https://codeday.me/bug/20191119/2039524.html