Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple database migration in one go

I have four different databases in my code first project which need migrations. Consider the following :

-- Enabling migrations
Enable-Migrations -StartUpProjectName SampleProject -MigrationsDirectory Migrations\DB1Configuration -ContextTypeName DB1ConfigurationDbContext -ContextAssemblyName SampleProject -ConnectionStringName MyDbContext1

Enable-Migrations -StartUpProjectName SampleProject -MigrationsDirectory Migrations\DB2Configuration -ContextTypeName DB2ConfigurationDbContext -ContextAssemblyName SampleProject -ConnectionStringName MyDbContext2

Enable-Migrations -StartUpProjectName SampleProject -MigrationsDirectory Migrations\DB3Configuration -ContextTypeName DB3ConfigurationDbContext -ContextAssemblyName SampleProject -ConnectionStringName MyDbContext3

Enable-Migrations -StartUpProjectName SampleProject -MigrationsDirectory Migrations\DB4Configuration -ContextTypeName DB4ConfigurationDbContext -ContextAssemblyName SampleProject -ConnectionStringName MyDbContext4

-- Addning migrations

Add-Migration -StartUpProjectName SampleProject -Name InitialCreate -ConfigurationTypeName SampleProject.Migrations.DB2Configuration.Configuration -ConnectionStringName MyDbContext1

Add-Migration -StartUpProjectName SampleProject -Name InitialCreate -ConfigurationTypeName SampleProject.Migrations.DB1Configuration.Configuration -ConnectionStringName MyDbContext2

Add-Migration -StartUpProjectName SampleProject -Name InitialCreate -ConfigurationTypeName SampleProject.Migrations.DB3Configuration.Configuration -ConnectionStringName MyDbContext3

Add-Migration -StartUpProjectName SampleProject -Name InitialCreate -ConfigurationTypeName SampleProject.Migrations.DB4Configuration.Configuration -ConnectionStringName MyDbContext4

--Create the database

Update-Database -StartUpProjectName SampleProject -ConfigurationTypeName SampleProject.Migrations.DB2Configuration.Configuration -ConnectionStringName MyDbContext1

Update-Database -StartUpProjectName SampleProject -ConfigurationTypeName SampleProject.Migrations.DB1Configuration.Configuration -ConnectionStringName MyDbContext2

Update-Database -StartUpProjectName SampleProject -ConfigurationTypeName SampleProject.Migrations.DB3Configuration.Configuration -ConnectionStringName MyDbContext3

Update-Database -StartUpProjectName SampleProject -ConfigurationTypeName SampleProject.Migrations.DB4Configuration.Configuration -ConnectionStringName MyDbContext4

Right now I need to run every single one of the above in Package Manager console to perform the migration however Ideally I would like to be able to put above in something like a script and run a single command to perform above operation. Is this possible (migrating multiple databases in one go)? Could you please provide the sample?

like image 661
MHOOS Avatar asked Jul 30 '15 15:07

MHOOS


2 Answers

You can use a power shell script for that.
Just copy all your commands to text file, assign and use arguments,set extension to ps1 and save to solution root folder.
Example UpdateAllDatabases.ps1:

$migrationName = $args[0]
Enable-Migrations -StartUpProjectName SampleProject -MigrationsDirectory Migrations\DB1Configuration -ContextTypeName DB1ConfigurationDbContext -ContextAssemblyName SampleProject -ConnectionStringName MyDbContext1
...
Add-Migration -StartUpProjectName SampleProject -Name $migrationName -ConfigurationTypeName SampleProject.Migrations.DB2Configuration.Configuration -ConnectionStringName MyDbContext1
...
Update-Database -StartUpProjectName SampleProject -ConfigurationTypeName SampleProject.Migrations.DB1Configuration.Configuration -ConnectionStringName MyDbContext1

Now you can execute script from Package Manager Console by just calling

.\UpdateAllDatabases.ps1 InitialCreate

like image 73
FireAlkazar Avatar answered Oct 18 '22 00:10

FireAlkazar


First Go to Enable-Migration for your Project, After that

Add-Migration -ConfigurationTypeName Sample_Project.Migrations.ApplicationDbContext.Configuration "InitialDatabaseCreation"


Update-Database -ConfigurationTypeName Sample_Project.Migrations.Project_SystemContext.Configuration

Change Dbcontext name as your project ,Same Code used for Multiple Migrations...

like image 31
Jegadeesh Waran Avatar answered Oct 18 '22 00:10

Jegadeesh Waran