Skip to content

Instantly share code, notes, and snippets.

@mrmyroll2
Last active August 4, 2025 05:55
Show Gist options
  • Select an option

  • Save mrmyroll2/e84054005a0fe5553ccfd187043a6f5e to your computer and use it in GitHub Desktop.

Select an option

Save mrmyroll2/e84054005a0fe5553ccfd187043a6f5e to your computer and use it in GitHub Desktop.
PowerShell script to automate EF Core migration and SQL script generation
<#
.SYNOPSIS
EF Core Migration Automation Script
.DESCRIPTION
Automates:
1. Creating a new EF Core migration
2. Inserting an <auto-generated /> tag into the migration file
3. Generating the corresponding SQL script
.INSTRUCTIONS
🔧 Place this script in the same folder as your DbContext (or near it for easy relative paths)
💻 Usage:
Open PowerShell, navigate to the script location (usually next to your DbContext):
cd path\to\your\dbcontext-folder
Then run:
.\generate-migration.ps1 YourMigrationNameHere
.NOTES
Update the following placeholders:
- $startupProject: path to your startup project's .csproj
- $dbContextProject: path to the project containing your DbContext
- $context: your actual DbContext class name
#>
param (
[Parameter(Mandatory = $true)]
[string]$MigrationName
)
# Paths
$startupProject = ".\..\..\..\..\{}.csproj" #Replace with actual csproj location
$dbContextProject = ".\..\{}Persistence.csproj" #Replace with actual csproj location
$migrationsFolder = Join-Path (Split-Path $dbContextProject -Parent) "Database\Migrations"
$context = "{contextName}DbContext" #Replace with dbcontext name
## 1. Run Migration
dotnet ef migrations add $MigrationName -c $context `
--project $dbContextProject `
--startup-project $startupProject `
-o Database\Migrations `
## 2. Auto-insert auto-generated comment
# Look for the newly created migration file
$latestFile = Get-ChildItem -Path $migrationsFolder -Filter "*_${MigrationName}.cs" |
Sort-Object Name -Descending |
Select-Object -First 1
if ($latestFile) {
$originalContent = Get-Content $latestFile.FullName
"// <auto-generated />" | Set-Content $latestFile.FullName
$originalContent | Add-Content $latestFile.FullName
Write-Host "✅ Inserted <auto-generated /> tag into: $($latestFile.Name)"
} else {
Write-Warning "⚠️ Could not find the migration file to mark as auto-generated."
}
## 3. Generate migration sql script
# Get the latest migration file (ignore snapshot/designer)
$migrationFile = Get-ChildItem $migrationsFolder -Filter "*.cs" |
Where-Object {
$_.Name -notmatch "ModelSnapshot\.cs$" -and
$_.Name -notmatch "\.Designer\.cs$"
} |
Sort-Object LastWriteTime |
Select-Object -Last 1
# Get the filename without extension to use as the SQL script name
$generatedMigrationFilename = [System.IO.Path]::GetFileNameWithoutExtension($migrationFile.Name)
# Final output SQL path
$outputPath = ".\..\Database\Migrations\Scripts\$generatedMigrationFilename.sql"
# Get previous migration from the second-latest .cs file
$allMigrations = Get-ChildItem $migrationsFolder -Filter "*.cs" |
Where-Object {
$_.Name -notmatch "ModelSnapshot\.cs$" -and
$_.Name -notmatch "\.Designer\.cs$"
} |
Sort-Object Name
$previousMigration = if ($allMigrations.Count -gt 1) {
[System.IO.Path]::GetFileNameWithoutExtension($allMigrations[-2].Name) -replace "^\d+_", ""
} else {
"0"
}
# Extract clean name for script generation
$currentMigration = $generatedMigrationFilename -replace "^\d+_", ""
Write-Host "Generating script from: $previousMigration -> $currentMigration"
# Generate SQL script
dotnet ef migrations script $previousMigration $currentMigration -c $context `
--project $dbContextProject `
--startup-project $startupProject `
-o $outputPath
Write-Host "✅ Migration '$MigrationName' added and script saved to:"
Write-Host $outputPath
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment