一次性进行多个数据库迁移

时间:2015-07-30 15:20:27

标签: c# entity-framework nuget ef-migrations

我的代码第一个项目中有四个不同的数据库需要迁移。请考虑以下事项:

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

现在我需要在Package Manager控制台中运行上面的每一个来执行迁移。但是,理想情况下,我希望能够将上面的内容添加到脚本中并运行单个命令来执行上述操作。这是可能的(一次迁移多个数据库)?你能提供样品吗?

2 个答案:

答案 0 :(得分:4)

您可以使用电源shell脚本。
只需将所有命令复制到文本文件,分配和使用参数,将扩展名设置为ps1并保存到解决方案根文件夹 示例 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

现在,您只需调用

即可从Package Manager控制台执行脚本
  

。\ UpdateAllDatabases.ps1 InitialCreate

答案 1 :(得分:1)

首先转到项目的启用迁移,之后

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


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

将Dbcontext名称更改为项目,使用相同代码进行多次迁移...