2015-05-29 79 views
2

我想在使用ServiceStack OrmLite的事務內執行SQL。下面的代碼適用於Sqlite,但不適用於SqlServer。使用SqlServer時,出現以下錯誤:ServiceStack OrmLite和事務

ExecuteScalar要求命令在分配給該命令的連接處於未決本地事務時執行事務。該命令的Transaction屬性尚未初始化。

這段代碼有什麼問題嗎?

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

回答

2

別人把這個答案的註釋,然後將其刪除...這樣:

的BeginTransaction必須OpenTransaction

+0

因爲我不能在當前的源代碼中找到'OpenTransaction'並沒有時間去嘗試代碼。你正在使用哪個版本? –

+0

正確的你需要使用'db.OpenTransaction()',在[OrmLite的主頁](https://github.com/ServiceStack/ServiceStack.OrmLite#transaction-support)上的例子。 – mythz