亚音速3交易

时间:2009-12-02 19:05:04

标签: subsonic

我按照MS网站上的说明配置了DTC,以支持远程交易。我有以下代码总是给我错误。

using (TransactionScope ts = new TransactionScope())
{
   Category c = new Category();
   c.Name = "Cat1";
   c.Save();

   Product p = Product.SingleOrDefault(x=>x.ProductID==1);
   p.Title = "new title";
   p.Save();

   ts.Close();
 }

但是,如果我将第二个代码块移出suing块,它就可以了。我想要做的是将这两个代码块绑定到一个trascation中。什么可以读取?谢谢,

1 个答案:

答案 0 :(得分:0)

您没有指定代码给您的错误,但我唯一看错的是您没有在TransactionScope上调用Complete。请尝试以下方法:

using (TransactionScope ts = new TransactionScope())
{
   Category c = new Category();
   c.Name = "Cat1";
   c.Save();

   Product p = Product.SingleOrDefault(x=>x.ProductID==1);
   p.Title = "new title";
   p.Save();

   ts.Complete();
 }

您实际上不需要启用DTC,您可以使用SubSonic的SharedDbConnectionScope将此代码包装在事务中。请尝试以下方法:

using (TransactionScope ts = new TransactionScope())
using (SharedDbConnectionScope sharedConnectionScope = new SharedDbConnectionScope())
{
   Category c = new Category();
   c.Name = "Cat1";
   c.Save();

   Product p = Product.SingleOrDefault(x=>x.ProductID==1);
   p.Title = "new title";
   p.Save();

   ts.Complete();
 }