实体框架4的事务问题

发布于 2024-09-25 13:13:36 字数 1387 浏览 5 评论 0原文

我正在尝试使用实体框架 4 实现事务。根据我所读到的内容,下面的代码是正确的。 SaveChanges 工作正常,但一旦我点击第一个 ExecuteFunction 调用,我就会收到以下异常:

底层提供程序失败 打开。 ---> System.Transactions.TransactionManagerCommunicationException: 分布式网络访问 事务管理器 (MSDTC) 已 禁用。请启用 DTC 网络访问的安全性 使用以下命令配置 MSDTC 组件服务管理 工具。

我已登录到数据库服务器,但没有看到名为“分布式事务管理器”的服务,但我确实看到了“分布式事务协调器”并且它已启动。我不确定我需要改变什么才能让它发挥作用。有人知道吗?谢谢。

这是代码。

using (var h = new WhaleEntities(ConnectionHelper.DBConnectString))
{
    using (TransactionScope ts = new TransactionScope())
    {
        h.Sites.AddObject(s);
        h.SaveChanges(SaveOptions.DetectChangesBeforeSave); 
        retval = s.SiteID;

        h.ExecuteFunction("UpdateSiteInterfaceList", new ObjectParameter("pSiteID", retval), new ObjectParameter("pList", "10"));
        h.ExecuteFunction("UpdateSiteInterfaceRequiredList", new ObjectParameter("pSiteID", retval),new ObjectParameter("pList", "Email"));
        h.ExecuteFunction("UpdateSiteInterfaceAlwaysShownList", new ObjectParameter("pSiteID", retval),new ObjectParameter("pList", "10"));
        h.ExecuteFunction("UpdateSiteInterfaceAlwaysRequiredList",new ObjectParameter("pSiteID", retval),new ObjectParameter("pList", "Email"));

        ts.Complete();

        //changes must be accepted manually once transaction succeeds.
        h.AcceptAllChanges();
    }
}

I'm trying to implement a transaction with entity framework 4. From what I've read, the code below is correct. The SaveChanges works fine but as soon as I hit the first ExecuteFunction call I get the following exception:

The underlying provider failed on
Open. --->
System.Transactions.TransactionManagerCommunicationException:
Network access for Distributed
Transaction Manager (MSDTC) has been
disabled. Please enable DTC for
network access in the security
configuration for MSDTC using the
Component Services Administrative
tool.

I've logged on to the database server and I don't see a service called Distributed Transaction Manager but I do see Distributed Transaction Coordinator and it is started. I'm not sure what I need to change to allow this to work. Anyone know? Thanks.

Here's the code.

using (var h = new WhaleEntities(ConnectionHelper.DBConnectString))
{
    using (TransactionScope ts = new TransactionScope())
    {
        h.Sites.AddObject(s);
        h.SaveChanges(SaveOptions.DetectChangesBeforeSave); 
        retval = s.SiteID;

        h.ExecuteFunction("UpdateSiteInterfaceList", new ObjectParameter("pSiteID", retval), new ObjectParameter("pList", "10"));
        h.ExecuteFunction("UpdateSiteInterfaceRequiredList", new ObjectParameter("pSiteID", retval),new ObjectParameter("pList", "Email"));
        h.ExecuteFunction("UpdateSiteInterfaceAlwaysShownList", new ObjectParameter("pSiteID", retval),new ObjectParameter("pList", "10"));
        h.ExecuteFunction("UpdateSiteInterfaceAlwaysRequiredList",new ObjectParameter("pSiteID", retval),new ObjectParameter("pList", "Email"));

        ts.Complete();

        //changes must be accepted manually once transaction succeeds.
        h.AcceptAllChanges();
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文