我开始使用StackExchange微型轮廓仪,并希望将它与oracle数据库一起使用。
但当我运行查询时会抛出异常-
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__的类。
在下面的方法中,当尝试转换为OracleCommand时会引发异常
void SqlMapper.IDynamicParameters.AddParameters(IDbCommand command, SqlMapper.Identity identity)
{
var oracmd = (OracleCommand)command; // exception!
this.AddParameters(oracmd, identity);
}怎么解决这个问题?
发布于 2016-08-03 08:55:34
我找到了解决问题的办法。
在StackExchange.Profiling.Data.ProfiledDbCommand中,似乎存在一个类型为OracleCommand的属性InternalCommand。从那里可以检索所有添加的参数。
修改代码后,它看起来如下所示:
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);
}https://stackoverflow.com/questions/38721088
复制相似问题