【问题标题】:Multiple databases (multiple DbContexts) in one transaction using TransactionScope使用 TransactionScope 在一个事务中的多个数据库(多个 DbContext)
【发布时间】:2016-07-06 23:42:17
【问题描述】:

我有两个 DbContext(一个是模型优先,另一个是代码优先),它们连接到 MSSQL 中的两个不同数据库。 现在,当您从任何类调用 SaveChanges 时,它会使用 TransactionScope 类同时将数据写入两个数据库。代码如下所示。

        using (TransactionScope scope = new TransactionScope())
        {
            using (Schema1Entities db1 = new Schema1Entities())
            {
                db1.SaveChanges();
            }
            using (Schema2Entities db2 = new Schema2Entities())
            {
                db2.SaveChanges();
            }
            scope.Complete();
        }

问题在运行时出现。据说

“System.Data.Entity.Core.EntityException”类型的异常 发生在 EntityFramework.SqlServer.dll 中但未在用户中处理 代码

附加信息:底层提供程序在打开时失败。

内部异常消息 - {“合作伙伴事务管理器有 禁用了对远程/网络事务的支持。 (例外来自 HRESULT: 0x8004D025)"}

已打开 MSDTC,并且没有防火墙阻止 MSDTC。 立即需要帮助。

【问题讨论】:

标签: c# sql-server entity-framework entity-framework-6


【解决方案1】:

根据问题下的 cmets,这解决了问题:

using (TransactionScope scope = new TransactionScope())
{
    using (Schema1Entities db1 = new Schema1Entities())
    using (Schema2Entities db2 = new Schema2Entities())
    {
        db1.SaveChanges();
        db2.SaveChanges();
    }
    scope.Complete();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多