6

I have an app set up right now to use EF6 Code-First Migrations. I use the standard workflow of Add-Migration followed by Update-Database in the Console. I use the MigrateDatabaseToLatestVersion initializer locally, as well as in our development environment. This handles all the migrations automatically for me and the other devs.

I'm uncomfortable with allowing the auto-migrations to take place in production, so I ran Update-Database -script to generate a SQL Script that I could review before running. This works really well and I'm fine with this process for deployments. However, I realized that the SEED method would never run because Update-Database isn't running directly on the database.

I'm looking for a good way to get the SEED method on the Migration Configuration to run without running the actual migrations. I found migrate.exe (http://msdn.microsoft.com/en-us/data/jj618307.aspx) which looks like it might work okay, but I wanted to see if anyone knows any better ways.

Also, and possibly more importantly, am I being ridiculous to worry about the Auto-Migration in production, considering how much automation I'm already inherently using by employing EF6??

Thanks!

Adam
  • 1,202
  • 11
  • 25
  • I left migrations on in production once by mistake and due to a bug in the migration and a limitation with our db hosting provider meaning all users had root access I lost the database. YMMV. – Matt Caton Jul 15 '14 at 18:43
  • 1
    I hope [this answer](http://stackoverflow.com/a/17339310/2115584) will help you – Baximilian Jul 15 '14 at 18:48
  • Thanks Baximilian. That looks like the answer I'm looking for. – Adam Jul 15 '14 at 18:59

2 Answers2

3

FYI - for those interested - I ended up creating my own database initializer that calls shared seed logic. I moved all the seed code to a static "Seed" class' execute method. I then created a simple DatabaseInitializer that I use in my production web.config. I still use MigrateToLatestVersion as my initializer in Dev and locally and it works like a charm.

public class SeedOnlyInitializer : IDatabaseInitializer<YourContextType>  {

    public void InitializeDatabase(YourContextType context)
    {
        Seed.Execute(context);
        context.SaveChanges();
    } 
}

Thanks to Baximilian for pointing me in the right direction. The answer didn't quite do what I wanted but it helped me come up with this.

Adam
  • 1,202
  • 11
  • 25
-1

Here's the workflow I just successfully used:

  1. update-database -script (as Adam did initially)
  2. review then run the SQL script (as Adam did initially)
  3. run update-database again (-script flag intentionally omitted this time). As long as your review in the prior step didn't involve any edits to the SQL script, then this step won't perform any additional migrations, and it will also run the Configuration::Seed() method.
dave_k_smith
  • 655
  • 1
  • 7
  • 22