Reputation: 1212
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!
Upvotes: 6
Views: 4444
Reputation: 693
Here's the workflow I just successfully used:
update-database -script
(as Adam did initially)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.Upvotes: -2
Reputation: 1212
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.
Upvotes: 3