2009-07-16 100 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?有人知道吗?