gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:12:53 25 4
gpt4 key购买 nike

我的代码优先项目中有四个不同的数据库需要迁移。考虑以下几点:

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com