When all changes for a new Version are made to the Entity c# Classes, the Package Manager Console Command "Add Migration (Migrationname)" creates a c# Class deriving from DbMigrations with a Up and a Down Method, which executes the necesarry SQL for Up/Downgrading the DB. The DB itself is upgraded either by the Package Manager Console Command Update-Database (which also lets you create script by -script Parameter) or by using Database Initializer, which are executed when the DBContext first Needs the Database:
//upgrades Production Datebase, maybe first make a backup
Database.SetInitializer(new MigrateDatabaseToLatestVersion
//creates new Db
Database.SetInitializer(new CreateDatabaseIfNotExists
Summary of Commands and Options: https://coding.abel.nu/2012/03/ef-migrations-command-reference/ or in the Package Manager Console: get-help Add-Migration
most important Package Manager Console Commands:
- Enable-Migrations
- Add Migration (Migrationname)
- Update-Database
Examples:
getting all migrations which have been deployed to a database so far:
get-migrations
adding a migration after changing EF-Relevant Code:
add-migration Mig0025
update Database to a specific Migration
Update-Database -TargetMigration Mig0005 -Verbose -connectionStringName Db1
No comments:
Post a Comment