9

Ich habe vier verschiedene Datenbanken in meinem ersten Code-Projekt, die Migrationen benötigen. Betrachten Sie die folgende:Mehrere Datenbankmigration auf einmal

-- 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 

Gerade jetzt muß ich jedes einzelne der oben in der Paket-Manager-Konsole ausführen, um die Migration durchzuführen jedoch Idealfall würde Ich mag Lage sein, oben wie ein Skript in etwas zu setzen und führe eine Einzelbefehl zum Ausführen der obigen Operation. Ist das möglich (mehrere Datenbanken gleichzeitig migrieren)? Könnten Sie bitte die Probe zur Verfügung stellen?

Antwort

4

Sie können ein Power Shell-Skript dafür verwenden.
Kopieren Sie einfach alle Ihre Befehle in eine Textdatei, weisen Sie Argumente zu und verwenden Sie sie, setzen Sie die Erweiterung auf ps1 und speichern Sie sie im Stammordner der Lösung.
Beispiel 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 

Jetzt können Sie Skript von Package Manager-Konsole ausführen, indem nur

Aufruf

\ UpdateAllDatabases.ps1 InitialCreate

0

Erste Zum Aktivieren -Migration für Ihr Projekt, danach

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


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

Ändern Dbcontext Name als Ihr Projekt, der gleiche Code für mehrere Migrationen verwendet ...