c# - 多数据库一口气迁移

标签 c# entity-framework nuget entity-framework-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

现在我需要在包管理器控制台中运行上述每一个来执行迁移,但是理想情况下我希望能够将上面的内容放在类似脚本的东西中并运行一个命令来执行上面的操作。这可能吗(一次迁移多个数据库)?能否请您提供 sample ?

最佳答案

您可以为此使用 power 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

现在您可以通过调用从程序包管理器控制台执行脚本

.\UpdateAllDatabases.ps1 InitialCreate

关于c# - 多数据库一口气迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31727775/

相关文章:

entity-framework - 我如何将现有实体移动到不同的 c# 命名空间并维护迁移?

tfs - NuGet 包仅在 TFS 管理的提要中显示为预发布

ASP.NET MVC 3 添加 Controller 抛出错误 "Object reference not set to an instance of object"

c# - 如何打包和部署带有符号和源代码的 NuGet 包,以便调试器可以使用该源代码?

c# - 是否有预定义的 ThisIsABugException?

c# - 在 Windows 窗体中动态调用函数

entity-framework - Entity Framework - 分离和重新附加实体?

c# - WinProcEvent 一个或多个 SetWinEventHook 回调?

c# - 检测鼠标是否以圆周方式移动

c# - 这个 ReSharper "Access to disposed closure"警告是否值得担心?