Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF6 Seed without Update-Database

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!

like image 941
Adam Avatar asked Oct 31 '22 20:10

Adam


1 Answers

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.

like image 159
Adam Avatar answered Nov 12 '22 12:11

Adam