15

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?

Marcelo Myara
  • 2,841
  • 2
  • 27
  • 36
  • possible duplicate http://stackoverflow.com/questions/7667630/can-you-create-sql-views-stored-procedure-using-entity-framework-4-1-code-firs – Eugene P. Mar 05 '14 at 11:37
  • Not duplicated, this is question is about code-based migration and the other one you point to is about code-first only. – E-Bat Mar 05 '14 at 11:55

4 Answers4

13

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.

David Sopko
  • 5,263
  • 2
  • 38
  • 42
9

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/

E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • Problem is: once i create a migration .cs file, with the partial class of DbMigration, the partial Up and Down methods (exactly as those created by the "add-migration" command) and then run the "update-database" it simply reports that "there's no pending code-based migrations"... :/ – Marcelo Myara Mar 05 '14 at 13:31
  • You have to create the file using add-migration as you would normally do. After that you will have an empty migration file, that is the one you have to edit the Up and Down methods like the sample code i provide. That way Update-Database will do its job. – E-Bat Mar 05 '14 at 14:07
1

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
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
0

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.

Jim Aho
  • 9,932
  • 15
  • 56
  • 87
  • Yes you're absolutely right. My primary intention was to clarify the fact that "add-migration" as stated by the questioner actually does exactly what the questioner claims it does not (I guess this behaviour must have changed since the time the question was written). I guess I can remove the part mentioning the years as that probably adds more confusion than it helps. – Jim Aho Jan 03 '23 at 09:36
  • I also wanted to add the fact that you can use the command line SDK `dotnet ef migrations add` if you (like me) use IDE's other than Visual Studio to work with .NET and EF. – Jim Aho Jan 03 '23 at 09:44