IT:AD:EF/CodeFirst/Migrations:HowTo:Update-Database
Summary
The Update-Database command applies “Coded Migrations” and “Automated Migrations” to the database.
Process
All you have to do is something like:
//Probably the most used scenario: // Generate an Incremental Script from last Applied Migration in Db till Current Code Model Update-Database -StartupProjectName:App.AppHost.Web -projectName:App.AppHost.Infrastructure -verbose
Regarding Update-Database and 'Automated Migrations'
If you have Enabled Automatic Migrations this is almost the easiest Migrations command to use.
This is because it handles the creation of Automatic Migrations before applying them, in effect making your development story:
- Make changes to an Entity Mapping
- Try to run your application
- Get the
Exceptionthat says the Model has changes:- “The model backing the ‘MyDatabaseContext’ context has changed since the database was created. Consider using Code First Migrations to update the database (http://go.microsoft.com/fwlink/?LinkId=238269).”
- Use the
Update-DatabaseIT:AD:Powershell command to:- automatically calculate and create Automated Migrations, and
- push the changes to our model to the database.
-
- Advantages:
- Nice and easy to get into Migrations – but it really is too easy (see Disadvantages below)
- Considerations:
- As it's so dangerous, consider letting go of the ease of “Automated Migrations”, and mastering “Code Migrations”, using IT:AD:EF/CodeFirst/Migrations:HowTo:Add-Migration
- Disadvantages:
- Dangerous!:
- Often generates Migrations that contain
DropTableandDropColumnwithout making any effort to copy the data first.- Not the kind of stuff that you want to slip into your code to be released to Production.