2009-07-16 76 views
3

所以這就是我迄今爲止。我做錯了什麼或在3.0.0.3中有錯誤嗎?Subsonic 3簡單的倉庫和交易

var Repository = new SimpleRepository("DBConnectionName"); 

    using (TransactionScope ts = new TransactionScope()) 
    { 
     using (SharedDbConnectionScope scs = new SharedDbConnectionScope("connstring", "providerName")) 
     { 
      try 
      { 
       for (int i = 0; i < 5; i++) 
       { 
        Supplier s = new Supplier(); 
        s.SupplierCode = i.ToString(); 
        s.SupplierName = i.ToString(); 

        Repository.Add<Supplier>(s); 
       } 

       ts.Complete(); 
      } 
      catch 
      { 
      } 
     } 
    } 

我越來越亞音速DbDataProvider 公衆的DbConnection CurrentSharedConnection { {返回__sharedConnection錯誤; }

 protected set 
     { 
      if(value == null) 
      { 
       __sharedConnection.Dispose(); 

等。 __sharedConnection == NULL :(對象空引用異常:(

回答

0

終於爲自己解決了這個問題。以上所有代碼都不適用於我(SubSonic 3.0.0.3,使用SQLite),但添加BeginTransaction()使其按預期工作,極大地加快了事務處理速度並在發生任何異常時回滾更新。

using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope(Access.Provider)) 
{ 
    using (IDbTransaction ts = sharedConnectionScope.CurrentConnection.BeginTransaction()) 
    { 
     IRepository repo = new SimpleRepository(Access.Provider); 
     //Do your database updates 

     //throw new ApplicationException("Uncomment this and see if the updates get rolled back"); 
     ts.Commit(); 
    } 
} 

爲了完整:Access.Provider是一個輔助類,我返回return SubSonic.DataProviders.ProviderFactory.GetProvider(ConnectionString, "System.Data.SQLite");

靜態屬性
0

也許切換SharedDbConnectionScope和TransactionScope的周圍可能會有幫助。

using (SharedDbConnectionScope scs = new SharedDbConnectionScope("connstring", "providerName")) 
{ 
    using (TransactionScope ts = new TransactionScope()) 
    { 
    } 
} 
0

這將遷移時發生已設置 - 在tablemigration上,dbconnection將被關閉。

嘗試SimpleRepository與SimpleRepositoryOption s.None。

不知道這是否是一個錯誤。我認爲這些交易對SimpleRepository不起作用,當在交易中拋出異常時,我總是保存一半的數據......也許這隻適用於ActiveRecord?有人知道嗎?