【问题标题】:Transient Errors using MySQL with .NET 5/EF将 MySQL 与 .NET 5/EF 结合使用的瞬态错误
【发布时间】:2021-07-21 20:28:57
【问题描述】:

我的 IT 人员刚刚将我们的 MySQL 数据库更新到了一个新集群,现在我遇到了类似以下的暂时性错误:

引发了一个异常,可能是由于暂时性故障。考虑通过将“EnableRetryOnFailure()”添加到“UseMySql”调用来启用瞬时错误恢复能力。

所以,经过研究,我添加了这段代码:

options.UseMySQL(Configuration.GetConnectionString(connectionString),
    options => options.EnableRetryOnFailure
        (
            maxRetryCount: 10,
            maxRetryDelay: TimeSpan.FromSeconds(30),
            errorNumbersToAdd: null
        )
    );
});

不幸的是,这导致了一个新的错误:

错误 CS1061“MySQLDbContextOptionsBuilder”不包含“EnableRetryOnFailure”的定义并且没有可访问的扩展方法“EnableRetryOnFailure”

我不知道我是否缺少参考,但智能感知没有帮助

所以,我在https://dev.mysql.com/doc/connector-net/en/connector-net-entityframework60.htmlhttps://entityframeworkcore.com/knowledge-base/57595183/using-entity-framework-with-multiple-databases-and-providers-in-the-same-project--sql-server-and-mysql- 进行了更多研究并找到有关使用 MySQL 的这一行的信息:

SetExecutionStrategy("MySql.Data.MySqlClient", () => new MySqlExecutionStrategy());

这也会引发错误。

有谁知道我如何使用 MySQL 实现连接弹性/重试逻辑?

.Net 5, MySQL 5.7, MySqlConnector 8.0.20

【问题讨论】:

    标签: mysql entity-framework-6 .net-5 transient-failure


    【解决方案1】:

    关注这个https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/implement-resilient-entity-framework-core-sql-connections

    使用

    services.AddDbContext<AppDbContext>(options =>
    {
        string connectionString = AppConfig.Configuration.GetConnectionString("DefaultConnection");
        options.UseMySql(connectionString,
            ServerVersion.AutoDetect(connectionString),
            mySqlOptions =>
                mySqlOptions.EnableRetryOnFailure(
                    maxRetryCount: 10,
                    maxRetryDelay: TimeSpan.FromSeconds(30),
                    errorNumbersToAdd: null);
            );
    });
    

    【讨论】:

    猜你喜欢
    • 2022-08-09
    • 1970-01-01
    • 2010-11-06
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    • 2011-05-28
    相关资源
    最近更新 更多