Search code examples
sql-servertransactionsormlite-servicestack

ServiceStack OrmLite and transactions


I am trying to execute sql inside a transaction using ServiceStack OrmLite. The code below works with Sqlite but not with SqlServer. With SqlServer I get the following error:

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

Is there something wrong with this code?

using (var trans = Db.BeginTransaction())
{
    try
    {
        foreach (myObject in myObjects)
            Db.Insert<MyObject>(myObject);
        trans.Commit();
    }
    catch (Exception ex)
    {
        trans.Rollback();
        throw ex;
    }
}

Solution

  • Someone else put this answer in a comment and then deleted it... so:

    BeginTransaction needs to be OpenTransaction