【问题标题】:Migration successful but not showing up in the database迁移成功但未显示在数据库中
【发布时间】:2021-11-12 19:09:23
【问题描述】:

我正在尝试为我的数据库进行迁移以添加一列。我设置了迁移,一切看起来都很好。我运行了命令,它抓取了正确的迁移并将其运行到数据库中,但它没有显示在数据库中。

我的迁移:

 using System;
 using Microsoft.EntityFrameworkCore.Migrations;

namespace Mudman.Data.Migrations
{
public partial class AttachedFileFileCategory : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {

        migrationBuilder.AddColumn<string>(
            name: "FileCategory",
            table: "AttachedFile",
            type: "nvarchar(max)",
            nullable: true);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "FileCategory",
            table: "AttachedFile");
    }
}

}

我的命令终端:

数据库:

【问题讨论】:

  • 您确定这是正确的数据库吗?另外,您是否刷新了用于连接数据库的应用程序?尝试关闭并重新打开它。
  • @DavidG 你说得对,它实际上进入了我们的阶段数据库,而不是我们的开发......将来有没有办法在添加列时指定你想要的数据库?
  • 它使用与上下文相同的数据库。如果它进入您的暂存数据库,那是因为那是通过运行具有环境配置的应用程序连接到的数据库 - 参见例如docs.microsoft.com/en-us/aspnet/core/fundamentals/…

标签: c# entity-framework-core


【解决方案1】:

我在 cmets 中读到您的“问题”是环境。
有很多方法可以满足您的需求:

  1. 您用于迁移的命令是Entity Framework Core tools 的一部分。 除其他事项外,文档提到您可以指定以下选项:

--connection &lt;CONNECTION&gt;
到数据库的连接字符串。默认为 AddDbContext 或 OnConfiguring 中指定的那个。在 EF Core 5.0 中添加。

示例:
dotnet ef database update your_migration --connection your_connection_string

  1. ConfigureServices 中,您可以获取环境并使用您想要的任何连接字符串有条件地注册dbContext。 How to get the Development/Staging/production Hosting Environment in ConfigureServices

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2017-09-25
    • 2021-07-02
    • 2020-07-13
    • 2020-12-16
    • 1970-01-01
    • 2014-04-27
    相关资源
    最近更新 更多