Skip to content

Instantly share code, notes, and snippets.

@pitermarx
Created May 4, 2023 13:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pitermarx/f792d87fabefa450717117ad0bde7aa7 to your computer and use it in GitHub Desktop.
Save pitermarx/f792d87fabefa450717117ad0bde7aa7 to your computer and use it in GitHub Desktop.
Script to generate EF migration scripts. One per migration
# Call this script in order to create migrations
$cnxStr = "Server=localhost;Database=Database;User Id=user;Password=password"
$migrationLocation = "..\SqlMigrations"
dotnet build
$mig = Read-Host "What's the name of the new migration? Leave empty if there is no new migration"
if ($null -ne $mig) {
dotnet ef migrations add $mig --no-build -- $cnxStr
dotnet build
}
Remove-Item "$migrationLocation\*.sql"
$migrations = dotnet ef migrations list --no-build --no-connect --json -- $cnxStr | ConvertFrom-JSON;
$from = "0"
$migrations | ForEach-Object {
$path = "$migrationLocation\$($_.id).sql"
$exists = Test-Path $path
if (-not $exists) {
dotnet ef migrations script $from $_.id -o $path --no-build --idempotent -- $cnxStr
}
$from = $_.id
}
public class DesignTimeFactory : IDesignTimeDbContextFactory<MyDbContext>
{
public MarketDataServiceDbContext CreateDbContext(string[] args)
=> new MyDbContext(new DbContextOptionsBuilder<MyDbContext>().UseSqlServer(args[0]).Options);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment