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

  • Verified answer
    Yegor Denysov Profile Picture
    on at
    RE: Using SqlCommand instead of using DatabaseContext in CRT

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

    In a meanwhile try using TransactionScope class with TransactionScopeAsyncFlowOption enabled.

  • Suhail Ismail Profile Picture
    103 on at
    RE: Using SqlCommand instead of using DatabaseContext in CRT

    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.

  • Suggested answer
    Sam.pruett Profile Picture
    10 on at
    RE: Using SqlCommand instead of using DatabaseContext in CRT

    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
    RE: Using SqlCommand instead of using DatabaseContext in CRT

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

  • ToddB Profile Picture
    on at
    RE: Using SqlCommand instead of using DatabaseContext in CRT

    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.

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

Ramesh Kumar – Community Spotlight

We are honored to recognize Ramesh Kumar as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Supply chain | Supply Chain Management, Commerce

#1
Laurens vd Tang Profile Picture

Laurens vd Tang 171 Super User 2025 Season 1

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 140 Super User 2025 Season 1

#3
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 127 Super User 2025 Season 1

Featured topics

Product updates

Dynamics 365 release plans