簡體   English   中英

無法更新數據庫以進行EF核心遷移

[英]Unable to Update Database for an EF Core Migration

我向項目添加了一個很小的遷移。 它只是將兩列的數據類型更改為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)");
    }
}

好的,問題是硬編碼到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);
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM