web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Supply chain | Supply Chain Management, Commerce
Answered

Using SqlCommand instead of using DatabaseContext in CRT

(0) ShareShare
ReportReport
Posted on by 103

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

I have the same question (0)
  • ToddB Profile Picture
    on at

    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.

  • Suhail Ismail Profile Picture
    103 on at

    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."

  • Suggested answer
    Sam.pruett Profile Picture
    10 on at

    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

  • Suhail Ismail Profile Picture
    103 on at

    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.

  • Verified answer
    Yegor Denysov Profile Picture
    on at

    We are investigating options to support DatabaseContext.BeginTransactionAsync() without persisted connection mode.

    In a meanwhile try using TransactionScope class with TransactionScopeAsyncFlowOption enabled.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Supply chain | Supply Chain Management, Commerce

#1
Laurens vd Tang Profile Picture

Laurens vd Tang 301 Super User 2025 Season 2

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 120 Super User 2025 Season 2

#3
Siv Sagar Profile Picture

Siv Sagar 105 Super User 2025 Season 2

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans