Reputation: 3011
Ok, so I'm relying completely on my migrations and seed code to maintain all database structure and initial data. Because of that, I'm facing a situation where all the changes I'm doing at this version are made directly on the database (Stored Procs and Updates) and nothing has changed on the C# code itself.
The question is: Since I want to do those DataBase specific changes using a new migration (and an "add-migration" will do nothing - cause the code hasn't change), how can I force a new empty code first migration to put my changes manually on it?
Upvotes: 16
Views: 23318
Reputation: 11887
Add-migration actually do exactly what's asked for.
You can just run dotnet ef migrations add
or Add-Migration
in the package manager console (as mentioned by David) to generate a class with empty Up
and Down
methods. Then just put your changes inside that class as usual.
Upvotes: 1
Reputation: 102408
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
Upvotes: 2
Reputation: 5643
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.
Upvotes: 14
Reputation: 4892
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/
Upvotes: 9