0

I'm using TransactionScope like this:

var efConnectionString = ConfigurationManager.ConnectionStrings["MainDbContext"].ConnectionString;

var transactionOptions = new TransactionOptions();
transactionOptions.IsolationLevel = IsolationLevel.ReadCommitted;
using (var scope = new TransactionScope(TransactionScopeOption.Required, transactionOptions))
{
    using (var conn = new EntityConnection(efConnectionString))
    {
        conn.Open();

        using (var context = new MainDbContext(conn, false))
        {

            // here are some actions, including EF data querying and modifications, and also context.Database.ExecuteSqlCommand();

            context.SaveChanges();
        }
    }

    scope.Complete();
}

When I view this in Glimpse console, transaction count is 0. If I check database, I can see that transaction has happened and commited. Am I doing something wrong? How can I see the correct number of transactions?

enter image description here

EDIT1:

MainDbContext connection string:

<add name="MainDbContext" connectionString="metadata=res://*/EF.EfDataModel.csdl|res://*/EF.EfDataModel.ss‌​dl|res://*/EF.EfDataModel.msl;provider=Oracle.DataAccess.Client;provider connection string=&quot;data source=SERVER;password=PASS;persist security info=True;user id=USER&quot;" providerName="System.Data.EntityClient" />
andree
  • 3,084
  • 9
  • 34
  • 42
  • What does your connection string look like and why are you wrapping an `EntityConnection` around a `DbContext`? – Justin Jun 16 '14 at 13:44
  • I added connection string. I'm using EntityConnection, because I need only 1 connection and I need to pass it to DbContext. More info here http://msdn.microsoft.com/en-us/data/dn456843.aspx and here http://stackoverflow.com/questions/6860979/dapper-transactionscope – andree Jun 16 '14 at 13:52
  • I thought you were using EF. EF provides a transaction mechanism on the Database property of the context. `DbContext.Database.BeginTransaction(IsolationLevel.ReadCommitted);`. Does this not do what you want? You are creating the transaction with the call and not getting it from elsewhere, so it doesn't make much sense not to use what EF already provides. EF also wraps all `SaveChanges()` calls in a transaction. – Justin Jun 16 '14 at 13:55
  • Yes, I'm using EF. BUT, I have Oracle db. Because of that, EF6 is not an option, I need to use EF5. DbContext.Database.BeginTransaction is not available in EF6. Also, I need to execute some pure SQL update statements, that cannot be included inside EF transaction. That's why I'm wrapping all with TransactionScope. – andree Jun 16 '14 at 14:00
  • It shouldn't be required, as EntityConnection should be using the DbProviderFactories, but have you tried wrapping the `EntityConnection` in a `new GlimpseDbConnection(new EntityConnection(efConnectionString))`? I run into issues all the time when working with Oracle db. – Justin Jun 16 '14 at 14:03
  • Tried that, it throws Unable to determine the provider name for connection of type 'Glimpse.Ado.AlternateType.GlimpseDbConnection'. – andree Jun 16 '14 at 14:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55711/discussion-between-justin-and-andree). – Justin Jun 16 '14 at 14:09

1 Answers1

0

The reason why you're not seeing any transactions in the Glimpse SQL Statistics and in the details for each executed query, is because you're using distributed transactions which are not taken into account.

If you want to see the number of transactions, then you must use the BeginDbTransaction method on the GlimpseDbConnection.

This is how Glimpse tracks the start of a new transaction and it knows when it ends when Commit or Rollback is called on the DbTransaction (a GlimpseDbTransaction that is) that is returned from the BeginDbTransaction method on the GlimpseDbConnection.

cgijbels
  • 5,994
  • 1
  • 17
  • 21