I am writing a code to perform a POST in my system. That post involves mainly two things
- Creation of user using UserManager (AspIdentity)
- 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?