Search code examples
c#oracle-databaseodp.netoracle.manageddataaccess

Share ManagedDataAccess.Client.OracleConnection connection/session/transaction with DataAccess.Client.OracleConnection


In my application, I am using a Repository class that has a public Oracle.ManagedDataAccess.Client.OracleConnection DbConn property. In another class where I use this repository, there's a particular use case where I need to use OracleBulkCopy's in order to quickly import data from an Access file, which unfortunately still isn't supported by the Oracle Managed nuget. Therefore, I need to instantiate a Oracle.DataAccess.Client.OracleConnection and create the OracleBulkCopy's based on that connection. But I'd like to keep the whole process inside a transaction, both the changes made through the repository's connection and the 'non-Managed' OracleConnection. The only way I can see that being done, is by 'sharing' the underlying connection/session/transaction in the repository's Oracle.ManagedDataAccess.Client.OracleConnection to the Oracle.DataAccess.Client.OracleConnection. Is sucha a thing possible?

Thanks in advance.


Solution

  • If you have a repository with a property

    Oracle.ManagedDataAccess.Client.OracleConnection DbConn
    

    You can abstract this by using the base class

    System.Data.Common.DbConnection DbConn
    

    Now, based on function you need, you can pass either Oracle.ManagedDataAccess.Client.OracleConnection or Oracle.DataAccess.Client.OracleConnection.

    Most functions will work with both, versions but BulkCopy will work only with unmanaged. So in your code, just do

    if (DbConn is Oracle.DataAccess.Client.OracleConnection conn)
    {
      //. . . . Do your bulk copy
    } 
    else
        throw new InvalidOperationException("Bulk copy supported only by un-managed ODP");