Using Migration DbMigration class for Up and Down action
Posted on: February 6, 2014
Entity Framework (EF) and Migration tools can be used in several ways. The most effective and complete one is to generate DbMigration class by the Entity Framework Migration Tools. It allows to have the creation of the table with all attributes like foreign key, size, primary key and so on. It allows to rollback in the situation that you have to get an older release. Often, this is possible with a source control but it is harder to have the database to be restored in time. Migration tools let you restore the database in the time which is a big plus. Also, it has the advantage to create a a database quickly which is very important for automatically testing or to simply create a new database for a developer. Migration also let you seed data into the database. Instead of having a database empty every time, you can have a database with minimal values.
Before talking about commands that you will use during the development. One command is required to be executed once. It is the one that configure the migration tool.
You can have help by using : get-help Enable-Migrations
You can specify the folder
First of all, there are commands that you will use often. You can create a solution items folder and create power shell scripts with them. This will simplify the use of them. In both case, these commands needs to be executed in the Package Manager Console.
1param([String]$migrationName)23Write-Host "MigrationName: $migrationName"45Add-Migration -Name $migrationName ` -ConfigurationType YourDLL.Migration.YourDBContextConfiguration ` -StartUpProjectName YourStartupDLL ` -ProjectName YourDLL.Migration ` -Verbose6``` Usage: ```7 .\\AddMigration.ps1 TheMigrationClassToBeGenerated
This first script calls the Add-Migration command. It takes a configuration type. This file inherit from DbMigrationsConfiguration which is generic. The generic type is the DbContext class. This DbContext must have all entities so if you have split your context is several files than you must create a specific one for the migration. Your DbMigrationsConfiguration allows you to override a seeding method. This is where you can specify values to be inserted into your tables. It works by giving in parameter the DbContext that let you insert entity and SaveChanges.
The project name is the parameter that let you specify the migration project. That should be the same project where you have the configuration type. The StatUpProjectName allows the migration tool to know where to search for config file. This is needed to get the connection string.
The power shell script let you configure all these setting once and then just let you specify the file name that contains the generated creation command. The file generated will be prefixed by the data and the time. This allows Entity Framework Migration tool to know the order of the execution of the file.
The second script is to update the database. Let say that your team has created some script with the Add-Migration. The one that executed the code is up-to-date but not you. This is why, to update your database, you need to get the code (from your source control) and then execute a command to update.
The update command is Update-Database. To make it simpler, like the Add-Migration, a small power script can be used.
1Update-Database -ConfigurationTypeName "YourDLL.Migration.YourDBContextConfiguration" ` -ProjectName "YourDLL.Migration" ` -StartUpProject "YourStartupDLL" ` -Verbose
Parameters are pretty much the same. This time, it will execute migration file. If you want to update not every file but just few files you can set the -TargetMigration. It takes after this command the file name to be the last file to be executed. If you have 10 migrations file and that you want to migrate up to the third file, than you need to target the third file. This can be useful if you need to rollback.
This is almost for it. You could remove all the files creation and use automatic migration. However, you have less control over what is executed. With migration file, you can always edit them to add additional instructions which is handy in the case of complex situation. This is almost all the time the case for enterprise application where rollback method needs to handle foreign key manipulations.