3

Is it possible to share one connection (and transaction as well) between operations made in EF6 DB First approach and some ADO operations?

As I read here: https://msdn.microsoft.com/en-us/data/dn456843.aspx#existing it is possible to share connection and transactions since EF v6, but I have to pass SqlConnection object as a parameter to the constructor of context class. But if I do that in DB First approach , I've got UnintentionalCodeFirstException.

As I read here: https://msdn.microsoft.com/en-us/data/jj592674 I have to use instance of EntityConnection as a parameter to the constructor of context class when DB First approach is used. But EntityConnection class constructor accept only CLOSED connections. And therefore I cannot share transaction, because connection has to be closed before passed to context class.

Am I right and it is impossible to share one connection in this scenario, or maybe there is some other way?

Is TransactionScope still the only way to wrap operations in EF 6 DB First and ADO in a transaction (with separate connections) ?

wlodziu
  • 81
  • 5
  • Could you elaborate a bit on why you want to share a connection? – Rob Tillie Jun 09 '15 at 13:16
  • @RobTillie In application I work with, part of code still use ADO, and other part use EF. I want to wrap operations in EF and ADO in a transaction. TransactionScope have some limitations, and MS recommend using one connection approach. Less connections is better as well. – wlodziu Jun 09 '15 at 13:32
  • You can hack this with reflection. I have done this with ef and sqlbulkcopy. Cant copy code at the moment. – Giorgi Nakeuri Jun 09 '15 at 22:21
  • I am guessing you are running stored procedures via ado.net? You can run stored procedures via your context. On another note, I have found when running both ado.net and ef in the same applications that it pays to have separate connections strings for when you are debugging possible database issues as you can see which connection string problems are stemming from. I know not the answer but couple of cents on the topic. – Bill Blankenship Jun 09 '15 at 22:33

1 Answers1

0

I can not remember where I did get this from, but it helped me with running EF and SqlBulkCopy in same transaction. Here is how to grab transaction with reflection from Entity Framework:

EntityConnection ec = (EntityConnection)Context.Connection;
SqlConnection sc = (SqlConnection)ec.StoreConnection;
SqlTransaction sqlTransaction = null;

var parserProp = sc.GetType().GetProperty("Parser", BindingFlags.NonPublic | BindingFlags.Instance);

if (parserProp != null)
{
    var parser = parserProp.GetValue(sc, null);
    var sqltxProp = parser.GetType().GetProperty("CurrentTransaction", BindingFlags.NonPublic | BindingFlags.Instance);
    var currentTransaction = sqltxProp.GetValue(parser, null);
    sqlTransaction = currentTransaction as SqlTransaction;

    if (sqlTransaction == null)
    {
        var parentProp = currentTransaction.GetType().GetProperty("Parent", BindingFlags.NonPublic | BindingFlags.Instance);
        currentTransaction = parentProp.GetValue(currentTransaction, null);
        sqlTransaction = currentTransaction as SqlTransaction;
    }
}
Giorgi Nakeuri
  • 35,155
  • 8
  • 47
  • 75