Entity framework 弹性和重试逻辑

Entity framework resiliency and retry logic

我在将数据添加到我的 Azure SQL 服务器时遇到很多错误,如下所示:

An exception has been raised that is likely due to a transient failure. If you are connecting to a SQL Azure database consider using

我可以在下面看到 link 你可以实施一个 azure 策略:

https://docs.microsoft.com/en-us/ef/ef6/fundamentals/connection-resiliency/retry-logic

文章中说明了您可以使用标准的 Azure 策略:

public class MyConfiguration : DbConfiguration
{
    public MyConfiguration()
    {
        SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
    }
}

但是它没有描述您如何将它应用到您的 DbContext。任何人都试过这个并且可以分享一些经验吗?它不是核心版本,它是在一个函数中实现的。在 9000 次调用中,大约有 1000 次失败,所以错误很多。

您可以在上下文 class 上使用 DbConfigurationType 属性,指定它是使用您的 MyConfiguration class:

[DbConfigurationType(typeof(MyConfiguration))]
public class DataContext : DbContext, IDataContext
{
    ...
}