2016-08-02 12 views
0

Ich begann mit der StackExchange mini profiler und wollte es mit Oracle-Datenbank verwenden.
Aber die Ausnahme wird ausgelöst, wenn ich die Abfrage-
Wie OracleCommand-Parameter von ProfiledDbCommand abgerufen werden

Unable to cast object of type 'StackExchange.Profiling.Data.ProfiledDbCommand' to type 'Oracle.ManagedDataAccess.Client.OracleCommand'.

Ich schaffe neue Verbindung:

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


Verfahren, bei dem ich die Abfrage ausführen:

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(); 

    //... 
} 

Hinweis: OraDynamicParams ist einfach eine Klasse, die von SqlMapper.IDynamicParameters erbt.

In folgenden Verfahren wird die Ausnahme ausgelöst, wenn Versuch OracleCommand würfen

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

Wie dieses Problem beheben?

Antwort

0

Ich fand die Lösung für mein Problem.

Es scheint, dass innerhalb der StackExchange.Profiling.Data.ProfiledDbCommand eine Eigenschaft InternalCommand ist, die vom Typ OracleCommand ist. Von dort können Sie alle hinzugefügten Parameter abrufen.

Nach dem Ändern des Codes sieht es in etwa so aus:

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); 
}