Code based migration is useful when you want more control on migration i.e. set default value of the column etc.
Code first has two commands for code based migration:
- Add-migration: It will scaffold the next migration for the changes you have made to your domain classes
- Update-database: It will apply pending changes to the database based on latest scaffolding code file you create using "Add-Migration" command
Assume that you have Student and Course entity classes initially and you want to use code based migration for your application. So before running above commands you must enable migration for your application by using enable-migrations commands in package manger as we did it for automatic migration. This will create configuration file same as automated migration. Also, you need to set database initializer in context class:
public class SchoolDBContext: DbContext { public SchoolDBContext(): base("SchoolDBConnectionString") { Database.SetInitializer(new MigrateDatabaseToLatestVersion<SchoolDBContext, SchoolDataLayer.Migrations.Configuration>("SchoolDBConnectionString")); } public DbSet<Student> Students { get; set; } public DbSet<Course> Courses { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { base.OnModelCreating(modelBuilder); } }
Now, you have to create scaffold code file which consist your database requirement from your existing domain classes. You can do this by running “add-migration" command in package manger. (from Tools → Library Package Manager → Package Manager Console). You have to pass the name parameter which will be part of code file name.
Add-Migration command Syntax:
Add-Migration [-Name] <String> [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] [-ConnectionStringName <String>] [-IgnoreChanges] [<CommonParameters>] Add-Migration [-Name] <String> [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] -ConnectionString <String> -ConnectionProviderName <String> [-IgnoreChanges] [<Common Parameters>]
You can see that this command has created new file in Migration folder with name parameter you passed in command with timestamp prefix:
After creating above file using add-migration command, you have to update the database. You can create or update the database using “update-database” command. You can use –verbose to see what’s going on in the database:
Update-Database command syntax:
Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] [-ConnectionStringName <String>] [<CommonParameters>] Update-Database [-SourceMigration <String>] [-TargetMigration <String>] [-Script] [-Force] [-ProjectName <String>] [-StartUpProjectName <String>] [-ConfigurationTypeName <String>] -ConnectionString <String> -ConnectionProviderName <String> [<CommonParameters>]
At this point, database will be created or updated.
Now suppose you added more domain classes. So before running application, you have to create scaffold file for new classes by executing "Add-Migration" command. Once it creates the file, update the database using Update-Database command. So this way you have repeat Add-Migration and Update-Database command each time when you make any changes in your domain classes.
Rollback Database change:
Suppose you want to rollback database schema to any of the previous state then you can use Update-database command with –TargetMigration parameter as below:
update-database -TargetMigration:"First School DB schema"
update-database -TargetMigration:"First School DB schema"
Use "get-migration" command to see what migration have been applied.
Note: Use get-help command for add-migration and update-database command to see what are the parameters we can pass with this command.
No comments:
Post a Comment