I did also have similar issue.
Suppose your initial migration creates a table, named 'SampleTable'
public partial class InitialCreate : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "SampleTable",
columns: table => new
{
...
},
constraints: table =>
{
table.PrimaryKey("PK_SampleTable", x => x.Id);
});
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "SampleTable");
}
}
And you want to drop that table. The steps you may need to follow are
- Remove model from dbcontext.
- Run command
dotnet ef migrations add RemoveSampleTable
(or any name of the migration). But it would generate only empty migration.
public partial class RemoveSampleTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
- And if you apply the migration by executing command like
dotnet ef database update
. It will basically do nothing. So the trick here is to copy the content of Up/Down from earlier migration and paste it to opposite method.
public partial class RemoveSampleTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "VesselBuildingProject");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "SampleTable",
columns: table => new
{
...
},
constraints: table =>
{
table.PrimaryKey("PK_SampleTable", x => x.Id);
});
}
}
- Now if you've already tried the migration on your db by now, you need to remove that entry from __EFMigrationsHistory.
- Now go ahead with executing command
dotnet ef database update
and see the magic :P