C# TransactionScope在某些地方有效,而在其他地方无效

C# TransactionScope在某些地方有效,而在其他地方无效,c#,sql-server-2005,linq-to-sql,transactionscope,msdtc,C#,Sql Server 2005,Linq To Sql,Transactionscope,Msdtc,在WindowsServer2003上使用ASP.NET3.5、LINQtoSQL、SQLServer2005。在本地XP SP3上运行VS 2008 我们需要能够在事务中包装插入、更新和删除。当我们第一次尝试使用(var trans=newtransactionscope()){…;trans.Complete();}将代码块包装为,我们得到一个适当的异常,告诉我们需要为远程事务启用网络访问。事情开始按我们预期的方式发展 快进到今天。我们的应用程序中有一个很少使用的部分也接受了Transact

在WindowsServer2003上使用ASP.NET3.5、LINQtoSQL、SQLServer2005。在本地XP SP3上运行VS 2008

我们需要能够在事务中包装插入、更新和删除。当我们第一次尝试使用(var trans=newtransactionscope()){…;trans.Complete();}将代码块包装为
,我们得到一个适当的异常,告诉我们需要为远程事务启用网络访问。事情开始按我们预期的方式发展

快进到今天。我们的应用程序中有一个很少使用的部分也接受了TransactionScope处理。尽管事务在我们的代码库的所有其他部分都正常工作,但我们今天发现,这个很少使用的部分引发了与以前相同的“网络访问”异常:

以下是导致异常的代码:

        using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
        {
            using (var dc = new ChargeXferDataContext())
            {
                //create 'Line' object and set initial values
                Line line = new Line();
                line.Unit_Num = UnitId;
                line.SubmittedBy = Viewer.Alias();
                line.LineSubmittedOn = DateTime.Now;

                //get codes to move from checked rows
                //iterate rows in current gridview
                foreach (GridViewRow row in gv.Rows)
                {
                    //if checked, insert move order
                    HtmlInputCheckBox cb = (HtmlInputCheckBox)row.FindControl("RowLevelCheckBox");
                    if (cb.Checked)
                    {

                        //1st: get required values
                        int id = Convert.ToInt32(((TextBox)row.FindControl("fldCodeId")).Text);
                        int newId = Convert.ToInt32(((DropDownList)row.FindControl("ddlNewId")).SelectedValue);
                        char newPOA = Convert.ToChar(((DropDownList)row.FindControl("ddlPOA")).SelectedValue);

                        //2nd: get current diag code from old patient
                        //######## Exception happens here...
                        DiagCode code = dc.DiagCodes.SingleOrDefault(c => c.Id == id);
                        //########

                        //3rd: add code to emenline object
                        addCode(line, code, newId, newPOA);

                    }
                }
                dc.SubmitChanges();
                trans.Complete();
            }
        }                       

如果您有任何建议,我们将不胜感激。如果我能解释更多,请告诉我。提前谢谢

addCode
方法做什么?它将code、newId和newPOA添加到line对象中。由于上面一行中的linq select,它永远无法添加代码()。抓住稻草,但是DiagCodes有什么不寻常的地方吗?链接到另一个数据库?做一些有趣的事情的存储过程?触发器?另外,我喜欢使用工厂生成数据上下文的模式,因此它们都是从应用程序中的一个位置创建的。不过,总的来说,我发现TransactionScope太脆弱了。下次我需要一个环境转换(在我当前的应用程序中很快就会出现),我将自己实现它。没有不可预测的MSDTC依赖项,而且它需要支持Oracle和SQL Server。