Hi am facing error when trying to use transaction with Databasecontext in CRT. Is it okay to use sqlcommand and sqlconnection in our custom processes
Hi am facing error when trying to use transaction with Databasecontext in CRT. Is it okay to use sqlcommand and sqlconnection in our custom processes
We are investigating options to support DatabaseContext.BeginTransactionAsync() without persisted connection mode.
In a meanwhile try using TransactionScope class with TransactionScopeAsyncFlowOption enabled.
Hi Sam,
Basically in the DatabaseContext there is no member called database or anything similar. I have used similar code to the link you shared in entity framework. In CRT it seems to be a different case.
Hey Suhail, this is because of the BeginTransactionAsync() method is opening a transaction against the same db context. Here are some ideas on how to get around that.
stackoverflow.com/.../using-begintransactionasync-in-different-functions-that-also-implement-it
Hi,
So as first step i am trying to create Database transaction as shown in the below code
try { using (var dbContext = new DatabaseContext(context)) { using (var dbTransaction = await dbContext.BeginTransactionAsync()) { var sqlQuery = new SqlQuery { QueryString = sql }; //setting parameters here await dbContext.ExecuteNonQueryAsync(sqlQuery); dbTransaction.Commit(); } } } catch(Exception ex) { throw ex; }
the above code throws error "This API is not supported in non-persisted connection mode.".
Now i have changed the code as below to allow persisted connection.
try { using (var dbContext = new DatabaseContext(context, DatabaseConnectionMode.IsPersisted)) { using (var dbTransaction = await dbContext.BeginTransactionAsync()) { var sqlQuery = new SqlQuery { QueryString = sql }; //parameters await dbContext.ExecuteNonQueryAsync(sqlQuery); dbTransaction.Commit(); } } } catch(Exception ex) { throw ex; }
with the above code it throws error "BeginExecuteReader requires the command to have a transaction when the connection assigned to the command is in a pending local transaction. The Transaction property of the command has not been initialized."
Hi Suhail,
I looked at all of our samples and it looks like Databasecontext is the way to go.
If you are unable to determine why things are not working using Databasecontext you could try posting in this forum for assistance.
Laurens vd Tang
171
Super User 2025 Season 1
André Arnaud de Cal...
140
Super User 2025 Season 1
Mohamed Amine Mahmoudi
127
Super User 2025 Season 1