Skip to content

Instantly share code, notes, and snippets.

@mbcrawfo
Last active May 18, 2024 17:02
Show Gist options
  • Save mbcrawfo/9b0568fa7c576dee4e8c4b1215a29f55 to your computer and use it in GitHub Desktop.
Save mbcrawfo/9b0568fa7c576dee4e8c4b1215a29f55 to your computer and use it in GitHub Desktop.
Helper script for the dotnet ef CLI
#!/bin/bash
# This script helps provide project specific options to the dotnet ef cli.
if [ $# -eq 0 ]; then
echo "Usage: ./dotnet-ef.sh <command> [options]"
exit 1
fi
args=(
--startup-project path/to/your/startup-app/startup-app.csproj
--project path/to/your/ef-project/ef-project.csproj
# Uncomment if your app has multiple DbContext's
# --context YourDbContext
)
if [[ $1 = "test-migration" ]]; then
if [[ $2 = "--help" ]]; then
echo "Automates the following process:"
echo "1. Create a migration named 'Test'"
echo "2. Apply the Test migration to your database"
echo "3. Revert the database to remove the Test migration"
echo "4. Delete the Test migration"
exit 0
fi
dotnet ef migrations add Test ${args[@]} || exit $?
echo
read -p "Test migration created - press enter to apply to database"
dotnet ef database update ${args[@]} || exit $?
echo
read -p "Database updated - press enter to clean up the test migration"
previousMigration=$(dotnet ef migrations list --prefix-output ${args[@]} | sed -rn 's/^data:\s+[0-9]{14}_(.+)$/\1/p' | tail -2 | head -1)
dotnet ef database update $previousMigration ${args[@]} || exit $?
dotnet ef migrations remove ${args[@]}
exit $?
fi
# Uncomment if you do not use the default Migrations namespace
# if [[ $1 = "migrations" && $2 = "add" ]]; then
# args+=(--namespace Your.Migrations.Namespace)
# fi
dotnet ef $@ ${args[@]} || exit $?
# Uncomment if you use csharpier to auto-format migrations and snapshots
# if [[ $1 = "migrations" && ($2 = "add" || $2 = "remove") ]]; then
# dotnet csharpier path/to/your/migrations || exit $?
# fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment