Created
May 4, 2023 13:22
-
-
Save pitermarx/f792d87fabefa450717117ad0bde7aa7 to your computer and use it in GitHub Desktop.
Script to generate EF migration scripts. One per migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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