2016-08-02 2 views
0

StackExchange mini profilerを使用し始め、oracleデータベースで使用したいと考えていました。
クエリを実行すると例外がスローされます。
ProfiledDbCommandからOracleCommandパラメータを取得する方法

Unable to cast object of type 'StackExchange.Profiling.Data.ProfiledDbCommand' to type 'Oracle.ManagedDataAccess.Client.OracleCommand'私は、クエリを実行する

this._oracleConnection = new OracleConnection(ConnectionString.Oracle); 
this._dbConnection = new StackExchange.Profiling.Data.ProfiledDbConnection(this._oracleConnection, MiniProfiler.Current); 


方法:

private long InsertData(SomeModel model, long someId, DbConnection conn) 
{ 
    OraDynamicParams insrtParams = this.GetSomeParams(model); 
    insrtParams.Add("a_some_id", someId); 
    insrtParams.Add("cur_OUT", dbType: OracleDbType.RefCursor, direction: ParameterDirection.Output); 
    dynamic res = conn.Query("SOME_CRUD.INSERT_PROC", insrtParams, commandType: CommandType.StoredProcedure).First(); 

    //... 
} 

注: OraDynamicParamsは単にSqlMapper.IDynamicParametersから継承するクラスである

は、私は新しい接続を作成します。

void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Identity identity) 
{ 
    var oracmd = (OracleCommand)command; // exception! 
    this.AddParameters(oracmd, identity); 
} 

これを修正する方法:OracleCommandにキャストしようとすると、

は、以下の方法で例外がスローされますか?

答えて

0

私の問題の解決策が見つかりました。

StackExchange.Profiling.Data.ProfiledDbCommandの内部には、タイプOracleCommandInternalCommandというプロパティがあるようです。そこから追加されたすべてのパラメータを取得できます。

コードを変更すると、次のようになります。

void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Identity identity) 
{ 
    // Checks if profiler is used, if it is, then retrieve `InternalCommand` property 
    dynamic oracmd = command is OracleCommand ? command : ((ProfiledDbCommand)command).InternalCommand; 
    this.AddParameters((OracleCommand)oracmd, identity); 
} 
関連する問題