0

I ran a migration (using Fluent Migrator) and just noticed that I made a mistake on the table name on the Foreign Key.

How can I fix that? Should I run a new migration?

public class AdditionalCosts : Migration
    {

        public override void Up()
        {
            Create.Table("AdditionalCosts").WithColumn("ID").AsInt32().PrimaryKey().Identity()
                  .WithColumn("DocTypeID").AsInt32().NotNullable()
                  .WithColumn("Cost").AsInt32().NotNullable()
                  .WithColumn("CreatedAt").AsDateTime().Nullable().WithDefault(SystemMethods.CurrentDateTime)
                  .WithColumn("AddedBy").AsString().Nullable()
                  .WithColumn("ModifiedAt").AsDateTime().NotNullable().WithDefault(SystemMethods.CurrentDateTime)

            Create.ForeignKey()
                .FromTable("AdditionalCosts").ForeignColumn("DocTypeID")
                .ToTable("Documents").PrimaryColumn("ID");

        }

        public override void Down()
        {
            Delete.Table("AdditionalFees");
        }

    }

The Foreign Key should be (different ToTable):

Create.ForeignKey()
     .FromTable("AdditionalCosts").ForeignColumn("DocTypeID")
     .ToTable("DocumentTypes").PrimaryColumn("ID");
Liam
  • 27,717
  • 28
  • 128
  • 190
user3378165
  • 6,546
  • 17
  • 62
  • 101
  • 1
    if you don't apply your migration you can revert, change your code and make a new migration – Paul Jan 29 '19 at 10:48
  • You need to [roll back you migration](https://stackoverflow.com/questions/8383858/rolling-back-to-previous-version-in-fluent-migrator). Then update your migration to the correct values then roll forward. Note the way you've set this migration up means any data in `AdditionalFees` will deleted. – Liam Jan 29 '19 at 10:59
  • @Liam Thank you, does that seem correct? `dotnet fm migrate -p sqlite -c "Data Source=test.db" -a FluentMigrator.Example.Migrations.dll down -t "migrationVersion"` How do I get the migration version? – user3378165 Jan 29 '19 at 11:22
  • Do you not have a `Migration` attribute on your class? You can check the `VersionInfo` table in your DB too – Liam Jan 29 '19 at 11:36
  • @Liam Thank you, I got it to work, do you want to write it as an answer so I can accept it? – user3378165 Jan 29 '19 at 14:43

1 Answers1

0

Thanks to @Liam I was able to roll back to the previous migration by running:

dotnet fm rollback -c ';Persist Security Info=True;User ID=test;Password=test; Initial Catalog=test;Data Source=.\SQLEXPRESS' -p SqlServer -a ./Project.Data/bin/Debug/netcoreapp2.2/Project.Data.dll to 20190123125705 --migration version
user3378165
  • 6,546
  • 17
  • 62
  • 101