4

I have pretty exotic need to modify code-first migration manually by adding SQL scripts. Scripts are not exactly a problem, but I would also like to inject into them some things that would be set in appsettings.json. Is it possible to access this configuration in some other way than navigating to file path and read the content manually?

Khaine
  • 295
  • 5
  • 18
  • 1
    i'm curious _why_ you want to do this - what problem are you trying to solve by this? maybe there's a more elegant solution to this. – Franz Gleichmann Nov 10 '20 at 17:55
  • I need to set different folder for MSSQL FileTables for different databases on the same server. There is rather no other way than doing this through SQL and I would rather like to keep that thing in migration. – Khaine Nov 10 '20 at 17:58
  • I have no idea whether there is a more elegant way of doing what you are trying to achieve, but anyways. Did you try injecting the IConfiguration interface in your migration? – Jordy Deweer Nov 11 '20 at 00:44
  • You can replace the `IMigrationsAssembly` service (https://stackoverflow.com/a/51730150/4139809) – Jeremy Lakeman Nov 12 '20 at 04:25

1 Answers1

6

Do you mean you want to get the appsettings value in the migration.cs file?

If this is your requirement, you could use ConfigurationBuilder and set the appsetting.json path to read it.

More details, you could refer to below codes:

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}";
    }
}

Result:

enter image description here

Brando Zhang
  • 22,586
  • 6
  • 37
  • 65
  • I will mark your comment as an answer because that's exactly how I resolved this problem and I would write the same thing as solution. BUT I need to add one thing. In my test project using integration tests (with WebHostBuilder, using a real database and so on) - at the time when migration should be applied to the database appsettings.json will not be from test project, but somehow it will be copied from Web Api. So in case of integration tests it's better to name appsettings.json differently than in project that we are testing. – Khaine Nov 12 '20 at 22:26