gpt4 book ai didi

entity-framework - 配置的执行策略 'SqlAzureExecutionStrategy'不支持用户发起的交易

转载 作者:行者123 更新时间:2023-12-02 09:39:16 24 4
gpt4 key购买 nike

我正在使用最新的 Entity Framework v6 以及 UnitOfWork 模式。过去几年这在服务器上一直很好。

我想迁移到 azure 托管并使用 SQLAzure,因此开始迁移应用程序。但是我遇到了很多问题。

首先,我不断间歇性地收到此错误

A transport-level error has occurred when receiving results from the server.

经过一番谷歌搜索后,这似乎很常见,您需要实现自己的 SqlAzureExecutionStrategy - 一切似乎都很好。直到我发现它不支持发起交易!

I then stumbled on this blog post - 其中概述了确切的问题并给出了如何解决问题的示例代码(或者我是这么认为的)。

我已完全按照该帖子进行操作(据我所知)。我有我的 dBconfiguration 类设置,它在应用程序启动时点击 SetExecutionStrategy。

public class EfConfig : DbConfiguration
{
public EfConfig()
{
SetExecutionStrategy("System.Data.SqlClient", () => SuspendExecutionStrategy
? (IDbExecutionStrategy)new DefaultExecutionStrategy()
: new CustomSqlAzureExecutionStrategy());
}

public static bool SuspendExecutionStrategy
{
get { return (bool?)CallContext.LogicalGetData("SuspendExecutionStrategy") ?? false; }
set { CallContext.LogicalSetData("SuspendExecutionStrategy", value); }
}
}

然后我有一个如上所述的自定义类,名为“CustomSqlAzureExecutionStrategy”,我将其放在下面并覆盖了 ShouldRetryOn 方法

public class CustomSqlAzureExecutionStrategy : SqlAzureExecutionStrategy
{
protected override bool ShouldRetryOn(Exception exception)
{
var shouldRetry = false;

var sqlException = exception as SqlException;
if (sqlException != null)
{
foreach (SqlError error in sqlException.Errors)
{
if (error.Number == -2)
{
shouldRetry = true;
}

}
}
shouldRetry = shouldRetry || base.ShouldRetryOn(exception);
return shouldRetry;
}
}

但是,当我运行我的应用程序时,我仍然遇到与开始时相同的错误,但这次只是指向自定义类?

The configured execution strategy 'CustomSqlAzureExecutionStrategy' does not support user initiated transactions.

我在这里错过了一些明显的东西吗?或者没明白什么?任何帮助将不胜感激。

更新

<小时/>

通常... StackOverFlow 橡皮鸭。我实际上正确地阅读了它,发现我需要在我的 UnitOfWork 中手动设置 SuspendExecutionStrategy (在 BeginTransaction 之前和 Commit 之后)。

所以我在 .BeginTransaction()

之前有这个
EfConfig.SuspendExecutionStrategy = true;

这就在 .Commit()

之后
EfConfig.SuspendExecutionStrategy = false;

这允许我现在运行应用程序,但我仍然(很少会添加)收到暂时性错误消息?

A transport-level error has occurred when receiving results from the server.

最佳答案

除了暂停执行策略之外,您还需要包装您正在重试的操作,包括手动调用执行策略中的事务,请参阅我对How to use SqlAzureExecutionStrategy and "Nolock"的回答

关于entity-framework - 配置的执行策略 'SqlAzureExecutionStrategy'不支持用户发起的交易,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30421695/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com