Browse Source

fix(migrations): 更新数据库上下文名称以支持迁移脚本生成

pull/1048/head
feijie 1 year ago
parent
commit
39095126e0
  1. 14
      aspnet-core/migrations/Migrate.ps1
  2. 14
      aspnet-core/migrations/MigrateEn.ps1

14
aspnet-core/migrations/Migrate.ps1

@ -13,11 +13,13 @@ $projectPath = Resolve-Path (Join-Path $PSScriptRoot "..")
$dbContexts = @{ $dbContexts = @{
"1" = @{ "1" = @{
Name = "LY.MicroService.Applications.Single.EntityFrameworkCore.MySql" Name = "LY.MicroService.Applications.Single.EntityFrameworkCore.MySql"
Factory = "SingleMySqlDbContextFactory" Context = "SingleMigrationsDbContext"
Factory = "SingleMigrationsDbContextFactory"
} }
"2" = @{ "2" = @{
Name = "LY.MicroService.Applications.Single.EntityFrameworkCore.PostgreSql" Name = "LY.MicroService.Applications.Single.EntityFrameworkCore.PostgreSql"
Factory = "SinglePostgreSqlDbContextFactory" Context = "SingleMigrationsDbContext"
Factory = "SingleMigrationsDbContextFactory"
} }
} }
@ -76,7 +78,7 @@ function Invoke-DatabaseMigration($dbContext, $migrationName) {
# 切换到项目目录并执行迁移 # 切换到项目目录并执行迁移
Push-Location $projectPath Push-Location $projectPath
try { try {
dotnet ef migrations add $migrationName --project "migrations/$($dbContext.Name)" --context "$($dbContext.Name)DbContext" dotnet ef migrations add $migrationName --project "migrations/$($dbContext.Name)" --context "$($dbContext.Context)"
} }
finally { finally {
Pop-Location Pop-Location
@ -87,7 +89,7 @@ function Invoke-DatabaseMigration($dbContext, $migrationName) {
function Get-AllMigrations($dbContext) { function Get-AllMigrations($dbContext) {
Push-Location $projectPath Push-Location $projectPath
try { try {
$migrations = @(dotnet ef migrations list --project "migrations/$($dbContext.Name)" --context "$($dbContext.Name)DbContext") $migrations = @(dotnet ef migrations list --project "migrations/$($dbContext.Name)" --context "$($dbContext.Context)")
return $migrations | Where-Object { $_ -match '\S' } # 过滤空行 return $migrations | Where-Object { $_ -match '\S' } # 过滤空行
} }
finally { finally {
@ -170,10 +172,10 @@ function Export-SqlScript($dbContext, $migrationName) {
Push-Location $projectPath Push-Location $projectPath
try { try {
if ($fromMigration) { if ($fromMigration) {
dotnet ef migrations script $fromMigration --project "migrations/$($dbContext.Name)" --context "$($dbContext.Name)DbContext" --output $sqlFilePath dotnet ef migrations script $fromMigration --project "migrations/$($dbContext.Name)" --context "$($dbContext.Context)" --output $sqlFilePath
} }
else { else {
dotnet ef migrations script --project "migrations/$($dbContext.Name)" --context "$($dbContext.Name)DbContext" --output $sqlFilePath dotnet ef migrations script --project "migrations/$($dbContext.Name)" --context "$($dbContext.Context)" --output $sqlFilePath
} }
} }
finally { finally {

14
aspnet-core/migrations/MigrateEn.ps1

@ -13,11 +13,13 @@ $projectPath = Resolve-Path (Join-Path $PSScriptRoot "..")
$dbContexts = @{ $dbContexts = @{
"1" = @{ "1" = @{
Name = "LY.MicroService.Applications.Single.EntityFrameworkCore.MySql" Name = "LY.MicroService.Applications.Single.EntityFrameworkCore.MySql"
Factory = "SingleMySqlDbContextFactory" Context = "SingleMigrationsDbContext"
Factory = "SingleMigrationsDbContextFactory"
} }
"2" = @{ "2" = @{
Name = "LY.MicroService.Applications.Single.EntityFrameworkCore.PostgreSql" Name = "LY.MicroService.Applications.Single.EntityFrameworkCore.PostgreSql"
Factory = "SinglePostgreSqlDbContextFactory" Context = "SingleMigrationsDbContext"
Factory = "SingleMigrationsDbContextFactory"
} }
} }
@ -76,7 +78,7 @@ function Invoke-DatabaseMigration($dbContext, $migrationName) {
# Switch to project directory and execute migration # Switch to project directory and execute migration
Push-Location $projectPath Push-Location $projectPath
try { try {
dotnet ef migrations add $migrationName --project "migrations/$($dbContext.Name)" --context "$($dbContext.Name)DbContext" dotnet ef migrations add $migrationName --project "migrations/$($dbContext.Name)" --context "$($dbContext.Context)"
} }
finally { finally {
Pop-Location Pop-Location
@ -87,7 +89,7 @@ function Invoke-DatabaseMigration($dbContext, $migrationName) {
function Get-AllMigrations($dbContext) { function Get-AllMigrations($dbContext) {
Push-Location $projectPath Push-Location $projectPath
try { try {
$migrations = @(dotnet ef migrations list --project "migrations/$($dbContext.Name)" --context "$($dbContext.Name)DbContext") $migrations = @(dotnet ef migrations list --project "migrations/$($dbContext.Name)" --context "$($dbContext.Context)")
return $migrations | Where-Object { $_ -match '\S' } # Filter empty lines return $migrations | Where-Object { $_ -match '\S' } # Filter empty lines
} }
finally { finally {
@ -170,10 +172,10 @@ function Export-SqlScript($dbContext, $migrationName) {
Push-Location $projectPath Push-Location $projectPath
try { try {
if ($fromMigration) { if ($fromMigration) {
dotnet ef migrations script $fromMigration --project "migrations/$($dbContext.Name)" --context "$($dbContext.Name)DbContext" --output $sqlFilePath dotnet ef migrations script $fromMigration --project "migrations/$($dbContext.Name)" --context "$($dbContext.Context)" --output $sqlFilePath
} }
else { else {
dotnet ef migrations script --project "migrations/$($dbContext.Name)" --context "$($dbContext.Name)DbContext" --output $sqlFilePath dotnet ef migrations script --project "migrations/$($dbContext.Name)" --context "$($dbContext.Context)" --output $sqlFilePath
} }
} }
finally { finally {

Loading…
Cancel
Save