In my CodeFirst application, local builds have a app.config flag that denotes not being in production. When I'm not in production it completely nukes and recreates the database. Since my production database user does NOT have permissions to drop the database even if my web.config transform is missed somehow (thus EF tries to recreate the database) my production database will not be deleted, and instead an exception will be thrown.
My workflow goes like this:
- Check out production branch of code with latest changes
- Quickly smoke/regression test (this should already be done prior to checking code into the production branch, but just in case)
- Download the latest backup of my production database and install it on my local SQLEXPRESS server
- Run Open DBDiff between the database my local code created (even though it's production code, since it's local it recreates the database) against the production backup.
- Review scripts generated and attempt to run them against the production backup
- Assuming no errors occurred, overwrite the database that the code generated with the production backup and do testing against the production data to make sure all data is intact still;
- Run scripts on the real production database.
Step #2 automatically creates a new, clean database based on the latest data model so I always know I have an up to date database that doesn't have artifacts from development efforts that may not be production ready yet.