How to force a new empty EF Migration?
This is a more up-to-date answer...
In Package Manager Console
in Visual Studio, add an empty migration targeting your Database context.
add-migration SeedingFacilityTable -context YourDbContextName
It'll create an empty migration provided you don't have any other DB changes to be applied.
Inside the Up
method, write the following:
public partial class SeedingFacilityTable : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.Sql(@"Put as many SQL commands as you want here");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
}
}
Then run the following command:
update-database -context YourDbContextName
In the package manager console issue the command
Add-Migration "My new empty migration"
This will generate this migration template
public partial class Mynewemptymigration : DbMigration
{
public override void Up()
{
}
public override void Down()
{
}
}
You can then create your own custom up and down migration steps. If you model is not up to date there will be migration code in the up and down. In that case you'll have to get your model up to date and then add a new empty migration.
You have to add an empty migration and add the code to the Up and Down method manually. I have found that people tend to think that the code for those methods have to be generated by the tool similar to ".designer" files and this is not the case. In fact more often than not i have found my self editing and adding code there. For this purpose I place all the sql code that i have to execute in scripts files and the execute then in the Up methods like this:
public override void Up(){
var dirBase = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\bin",string.Empty) + @"\Migrations\SqlScripts";
Sql(File.ReadAllText(dirBase + @"\CreateMyViews.sql"));
Sql(File.ReadAllText(dirBase + @"\CreateMySproc.sql"));
}
public override void Down(){
var dirBase = AppDomain.CurrentDomain.BaseDirectory.Replace(@"\bin",string.Empty) + @"\Migrations\SqlScripts";
Sql(File.ReadAllText(dirBase + @"\DropMySproc.sql"));
Sql(File.ReadAllText(dirBase + @"\DropMyViews.sql"));
}
I recomend you read this link: http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/