【问题标题】:Unable to Update Database for an EF Core Migration无法为 EF Core 迁移更新数据库
【发布时间】:2018-09-21 19:41:20
【问题描述】:

我在我的项目中添加了一个非常小的迁移。它只是将几列的数据类型更改为decimal(5,2)Add Migration 命令生成迁移类。但是,Update-Database 命令会引发异常。

另外,Remove-Migration 也会抛出同样的异常:

引发了一个异常,可能是由于暂时性故障。如果您要连接到 SQL Azure 数据库,请考虑使用 SqlAzureExecutionStrategy。

我正在使用本地 SQL Server 2014 数据库。我用于开发目的的连接字符串非常简单:

"DbConnectionString": "Server=.;Database=DBNAME;Trusted_Connection=True;MultipleActiveResultSets=true"

存储在config.json 文件中。我正在使用最新版本的 Visual Studio,即 VS 2017 15.8.5。

我将 Web 项目更新为 .NET Core 2.1,然后在 Nuget 中升级到最新的稳定版本 2.1.4。这导致错误消息发生变化,现在是:

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

我去了我的创业班,我确实添加了 EnableRetryOnFailure 属性,尽管我怀疑这是问题的根源。

services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("DbConnectionString"), builder =>
        {
            builder.EnableRetryOnFailure(5, TimeSpan.FromSeconds(10), null);
        }));

生成的迁移类:

public partial class v50 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<decimal>(
            name: "LnRrSncsm",
            table: "Jobs",
            type: "decimal(5, 2)",
            nullable: false,
            oldClrType: typeof(float));

        migrationBuilder.AlterColumn<decimal>(
            name: "LnRrQsnqcsm",
            table: "Jobs",
            type: "decimal(5, 2)",
            nullable: false,
            oldClrType: typeof(float));
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<float>(
            name: "LnRrSncsm",
            table: "Jobs",
            nullable: false,
            oldClrType: typeof(decimal),
            oldType: "decimal(5, 2)");

        migrationBuilder.AlterColumn<float>(
            name: "LnRrQsnqcsm",
            table: "Jobs",
            nullable: false,
            oldClrType: typeof(decimal),
            oldType: "decimal(5, 2)");
    }
}

【问题讨论】:

  • 可以看到生成的迁移文件吗?
  • 刚刚添加了生成的迁移文件。
  • 看不到迁移文件有问题。也许你会在这个相关的堆栈溢出问题上找到一些有用的东西:https://stackoverflow.com/questions/29840282/error-when-connect-database-continuously
  • 我没有使用 AzureSQL,但是,在我写这个问题之前,我已经遇到了那个线程,并且我已经在我的 Startup 类中添加了 EnableRetryOnFailure,所以我想我已经掩盖了这种可能性。此外,这不是防火墙问题。没有什么可以阻止 Web 应用程序与数据库通信。问题在于此迁移(或此时的任何迁移)。
  • 我从我的连接字符串中的 Trusted_Security=true 切换,现在我得到一个不同的错误。建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确以及 SQL Server 是否配置为允许远程连接。 (提供者:SQL 网络接口,错误:26 - 错误定位服务器/指定的实例)。

标签: sql-server entity-framework asp.net-core


【解决方案1】:

好的,问题是硬编码到 AppDbContextFactory 中的连接字符串。解决方案是改用配置。

public AppDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder<AppDbContext>();
        optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DbConnectionString"), opts => opts.CommandTimeout((int)TimeSpan.FromMinutes(10).TotalSeconds));

        return new AppDbContext(optionsBuilder.Options);
    }

【讨论】:

    猜你喜欢
    • 2020-07-09
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 2019-08-03
    • 1970-01-01
    • 2021-09-16
    • 2022-09-29
    • 2019-07-14
    相关资源
    最近更新 更多