ca9163d9
ca9163d9

Reputation: 29209

Manually add a migration?

I've been using Entity framework code first in a project and all the tables have been created /modified a while ago. Now I need to add an unique constraint to a table. I want to create a migration which will have the following row in the Up() method. And there is no change on the model classes.

CreateIndex("TableName", new[] { "Column1" }, true, "IX_UniqueKey");

And maybe the following row in the Down()

DropIndex("TableName", new [] { "Column1" });

Can I just manually create a file under the folder Migrations? And is there any mandatory file name convention?

How to create the .Designer.cs and .resx files? Or should I use add-migration with some parameters?

Upvotes: 15

Views: 13449

Answers (2)

Bart De Boeck
Bart De Boeck

Reputation: 826

You can manually add a migration with the following template (belongs in your Migrations folder):

  /// <inheritdoc />
  [DbContext(typeof(YourDbContext))]
  [Migration("20230917114131_MyMigration")]
  public partial class MyMigration : Migration
  {
      /// <inheritdoc />
      protected override void Up(MigrationBuilder migrationBuilder)
      {

      }

      /// <inheritdoc />
      protected override void Down(MigrationBuilder migrationBuilder)
      {
          
      }
  }

Upvotes: 1

Thewads
Thewads

Reputation: 5063

Using add-migration is the way for this. Just use add-migration yourMigrationName and it will automatically create all the files you need.

Then you just alter the Up() and Down() methods with the values that you need

Upvotes: 21

Related Questions