Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server Azure SQL和本地SQL server之间的分布式事务会导致错误_Sql Server_Azure Sql Database_Transactionscope_Msdtc - Fatal编程技术网

Sql server Azure SQL和本地SQL server之间的分布式事务会导致错误

Sql server Azure SQL和本地SQL server之间的分布式事务会导致错误,sql-server,azure-sql-database,transactionscope,msdtc,Sql Server,Azure Sql Database,Transactionscope,Msdtc,是否可以在本地SQL Server实例和Azure SQL数据库之间进行事务处理 我有以下测试用例 public class TransactionsTest { [Fact] public void Test1() { var premisesDatabaseContext = new OnPremisesDatabaseContext(); var azureSQLDatabaseContext = new AzureSQLDataba

是否可以在本地SQL Server实例和Azure SQL数据库之间进行事务处理

我有以下测试用例

public class TransactionsTest
{
    [Fact]
    public void Test1()
    {
        var premisesDatabaseContext = new OnPremisesDatabaseContext();
        var azureSQLDatabaseContext = new AzureSQLDatabaseContext();

        using (TransactionScope scope = new TransactionScope())
        {
            premisesDatabaseContext.Database.Connection.Open();
            azureSQLDatabaseContext.Database.Connection.Open();

            scope.Complete();
        }
    }

    [Fact]
    public void Test2()
    {
        var premisesDatabaseContext = new OnPremisesDatabaseContext();
        var azureSQLDatabaseContext = new AzureSQLDatabaseContext();

        using (TransactionScope scope = new TransactionScope())
        {
            azureSQLDatabaseContext.Database.Connection.Open();
            premisesDatabaseContext.Database.Connection.Open();

            scope.Complete();
        }
    }
}
这似乎很简单。但是当我打开第二个连接时,两个测试用例都失败了,并出现了不同的错误

以下是错误详细信息

// TEST 1
System.Reflection.TargetInvocationException: 
Exception has been thrown by the target of an invocation. 
--->
System.NullReferenceException: Object reference not set to an instance of an object.
       at System.Transactions.Transaction.GetPromotedToken()

// TEST 2
System.Transactions.TransactionPromotionException:
There is a promotable enlistment for the transaction which has a PromoterType value that is not recognized by System.Transactions. 
1c742caf-6680-40ea-9c26-6b6846079764
我想知道是否有可能做到这一点。如果不可能,有哪些替代方案?

您不能 Azure sql数据库使用。而本地服务器使用Microsoft分布式事务协调器(MSDTC)

azure上不支持MSDTC,根据弹性事务文档

仅支持SQL DB中跨数据库的事务。SQL DB之外的其他X/Open XA资源提供程序和数据库不能参与弹性数据库事务这意味着弹性数据库事务不能跨越本地SQL Server和Azure SQL数据库。对于本地分布式事务,请继续使用MSDTC

  • 本地SQL Server使用MSDTC,这在Azure上不可用 SQL数据库

  • Azure SQL数据库使用弹性事务,这不适用于 本地SQL server

最简单的解决方案是将所有数据库移动到Azure或内部部署