【问题标题】:Can I access appsettings from EF Core migration?我可以从 EF Core 迁移访问 appsettings 吗?
【发布时间】:2021-02-22 16:49:58
【问题描述】:

我非常需要通过添加 SQL 脚本手动修改代码优先迁移。脚本并不完全是个问题,但我还想在它们中注入一些将在 appsettings.json 中设置的东西。除了导航到文件路径并手动读取内容之外,是否可以通过其他方式访问此配置?

【问题讨论】:

  • 我很好奇为什么你想这样做 - 你想通过这个解决什么问题?也许有一个更优雅的解决方案。
  • 我需要为同一服务器上的不同数据库的 MSSQL FileTables 设置不同的文件夹。除了通过 SQL 执行此操作之外别无他法,我宁愿将其保留在迁移中。
  • 我不知道是否有更优雅的方式来做你想要实现的目标,但无论如何。您是否尝试在迁移中注入 IConfiguration 接口?
  • 可以替换IMigrationsAssembly服务(stackoverflow.com/a/51730150/4139809

标签: c# asp.net-core entity-framework-core


【解决方案1】:

您的意思是要获取 migration.cs 文件中的 appsettings 值吗?

如果这是您的要求,您可以使用ConfigurationBuilder 并设置 appsetting.json 路径来读取它。

更多细节,您可以参考以下代码:

public partial class Modify5 : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "Msg",
            table: "Logs");

        migrationBuilder.DropColumn(
            name: "Timestamp",
            table: "Logs");

        migrationBuilder.AddColumn<DateTime>(
            name: "CreatedDate",
            table: "Logs",
            nullable: false,
            defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

        migrationBuilder.AddColumn<DateTime>(
            name: "CreatedDate2",
            table: "Logs",
            nullable: false,
            defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

        migrationBuilder.AddColumn<string>(
            name: "Message",
            table: "Logs",
            nullable: true);

        string re = GetParameters();
        Console.WriteLine("Save");
        Console.WriteLine(re);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "CreatedDate",
            table: "Logs");

        migrationBuilder.DropColumn(
            name: "CreatedDate2",
            table: "Logs");

        migrationBuilder.DropColumn(
            name: "Message",
            table: "Logs");

        migrationBuilder.AddColumn<string>(
            name: "Msg",
            table: "Logs",
            type: "nvarchar(max)",
            nullable: true);

        migrationBuilder.AddColumn<DateTime>(
            name: "Timestamp",
            table: "Logs",
            type: "datetime2",
            nullable: false,
            defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified));

  
    }

    private static string GetParameters()
    {
        var builder = new ConfigurationBuilder()
                            .SetBasePath(@"the application root path")
                            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        var val1 = builder.Build().GetSection("AllowedHosts").Value;

        return $"The values of parameters are: {val1}";
    }
}

结果:

【讨论】:

  • 我会将您的评论标记为答案,因为这正是我解决此问题的方式,我将编写与解决方案相同的内容。但我需要添加一件事。在我使用集成测试的测试项目中(使用 WebHostBuilder,使用真实数据库等) - 在迁移应用到数据库时 appsettings.json 不会来自测试项目,但会以某种方式从 Web Api 复制.因此,在集成测试的情况下,最好将 appsettings.json 命名为与我们正在测试的项目中不同的名称。
猜你喜欢
  • 2021-10-08
  • 1970-01-01
  • 2017-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多