2016-08-30 2 views
2

지원 방법이 있는지 모르겠지만 Dapper가 문자열 매개 변수 값을 사용중인 것으로 보이는 Postgresql citext 데이터 유형에 매핑하는 데 문제가 있습니다. 텍스트 형 특히 Dapper 및 Postgresql-citext 데이터 유형 사용

, 나는 citext의 매개 변수를받는 함수를 호출하려고 해요 - 내가 돌아올 오류 것은 :

var c = ConnectionManager<T>.Open(); 
string sql = @"select * from ""dbo"".""MyFunction""(@schemaName, @tableName);"; 
var param = new 
{ 
    schemaName = schema, 
    tableName = table 
}; 

string insecureSalt = c.QueryMultiple(sql, param).Read<string>().FirstOrDefault(); 
ConnectionManager<T>.Close(c); 

Error: Npgsql.PostgresException: 42883: function dbo.MyFunction(text, text) does not exist. 

일치합니다 서명 기능의 dbo.MyFunction입니다 (citext, citext) 그래서 그것은 분명히 기본 매핑을 사용하여 찾을 수 없습니다.

Npgsql - http://www.npgsql.org/doc/types.html에 따르면 형식으로 NpgsqlDbType.Citext를 지정할 수 있어야하지만 Dapper를 사용하여이 작업을 수행 할 수있는 방법을 찾을 수 없습니다.

는 셰이에서 여기에 완벽한 솔루션을 대답 덕분에 해결 :

당신은 아마 ICustomQueryParameter를 확장하는 CitextParameter을 만들 만들 필요가
var c = ConnectionManager<T>.Open(); 
string sql = @"select * from ""dbo"".""MyFunction""(@schemaName, @tableName);"; 
var param = new 
{ 
    schemaName = new CitextParameter(schema), 
    tableName = new CitextParameter(table) 
}; 

string insecureSalt = c.QueryMultiple(sql, param).Read<string>().FirstOrDefault(); 
ConnectionManager<T>.Close(c); 

public class CitextParameter : SqlMapper.ICustomQueryParameter 
{ 
    readonly string _value; 

    public CitextParameter(string value) 
    { 
     _value = value; 
    } 

    public void AddParameter(IDbCommand command, string name) 
    { 
     command.Parameters.Add(new NpgsqlParameter 
     { 
      ParameterName = name, 
      NpgsqlDbType = NpgsqlDbType.Citext, 
      Value = _value 
     }); 
    } 
} 

답변

4

. 이 API를 사용하면 임의의 DbParameter 인스턴스를 Dapper에 전달할 수 있습니다.이 경우 NpgsqlDbType이 Citext로 설정된 NpgsqlParameter의 인스턴스가됩니다. 이 같은

뭔가 작동합니다 : 당신은 SQL 쿼리를 작성하는 경우

class CitextParameter : SqlMapper.ICustomQueryParameter 
{ 
    readonly string _value; 

    public CitextParameter(string value) 
    { 
     _value = value; 
    } 

    public void AddParameter(IDbCommand command, string name) 
    { 
     command.Parameters.Add(new NpgsqlParameter 
     { 
      ParameterName = name, 
      NpgsqlDbType = NpgsqlDbType.Citext, 
      Value = _value 
     }); 
    } 
} 
+0

이 완벽 당신을 감사 작동하는 경우, 당신은 다음과 같은 쿼리를 변경하고 볼 수 있습니다 (USR는 클래스 객체이다)! 이것에 구글 히트 곡이 없었기 때문에 영원히 찾아 낼 수있었습니다. –

0

, 당신은 (citext로 @param) 캐스트와 같은 매개 변수 값을 캐스팅 할 수 있습니다.

내 경우 올바르게 작동합니다.

귀하의 경우에는
string sql = "select * from users where user_name = cast(@user_name as citext) and password = @password;"; 
IEnumerable<users> u = cnn.Query<users>(sql, usr); 

string sql = @"select * from ""dbo"".""MyFunction""(cast(@schemaName as citext), cast(@tableName as citext));";