Transaction in ORMLite

I am writing a code to perform a POST in my system. That post involves mainly two things

  1. Creation of user using UserManager (AspIdentity)
  2. Some insert in my own tables using SQL queries.

So the main problem which is arising for me is that I want to execute all the code inside transaction for which I have been following the Database Transactions guide. But the UserManager operation uses AspIdentity dbcontext which is different from ORMLite. So right now I have to create two transaction one for AspIdentity operation and one ORMLite transaction.

var dbContext = services.GetRequiredService<ApplicationDbContext>();
using (var connection = dbContext.Database.GetDbConnection())
{
    await connection.OpenAsync();
    using (var transaction = await connection.BeginTransactionAsync())
    {
        dbContext.Database.UseTransaction(transaction);
        using (var trans = Db.OpenTransaction())
        {  
               try
               {

                    // UserManager operation
                    // Custom SQL Queries
                    trans.Commit();
                    await transaction.CommitAsync();
               }
               catch(Exception ex)
               {
                    trans.Rollback();
                    await transaction.RollbackAsync();
               }
        }
    }
}

Because with just ORMLite transaction my Identity changes done by the UserManager wont rollback in case of failure and if I use only dbContext transaction then my custom sql queries changes wont rollback.

Is there any better way to do it or a way where I can use single transaction?

I’ve just added support for using an existing DB Connection and Transaction in OrmLite with:

var db = dbFactory.Use(connection,transaction);

Note: you shouldn’t use this in a using or call Dispose() on this db since it doesn’t own the connection or transaction.

This change is available from the latest v8.6.1+ that’s now available in the pre release packages.