Testing DBContext migrations

Today i was creating EntityFrameworkCore DBContexts and it’s a time consuming work to delete the databases, add migrations and then update the database to see what the migration scripts would generate.

I created a simple powershell-script that i ran when needed.

Write-Host "Deleting the migrations folder..."
Remove-Item "Migrations" -Force -Recurse

Write-Host "Dropping database ASPD.."
sqlcmd -E -S "np:\\.\pipe\LOCALDB#3786839C\tsql\query" -Q "use master; ALTER DATABASE [ASPD] SET SINGLE_USER WITH ROLLBACK IMMEDIATE;DROP DATABASE [ASPD];"

Write-Host "Adding initial create for contexts.."
add-migration InitialCreate -Context "CustomerContext" -OutputDir "Migrations/Customers"
add-migration InitialCreate -Context "ProductContext" -OutputDir "Migrations/Products"
add-migration InitialCreate -Context "PartContext" -OutputDir "Migrations/Parts"

Write-Host "Updating database for each context.."
update-database -Context "CustomerContext"
update-database -Context "ProductContext"
update-database -Context "PartContext"

To find the instance pipe name of the LocalDB i used the sqllocaldb utility.

PS > sqllocaldb info ProjectsV13
Name:               ProjectsV13
Version:            13.1.4001.0
Shared name:
Owner:              DOMAIN\DEVJUNKIE
Auto-create:        No
State:              Running
Last start time:    2017-08-24 09:33:38
Instance pipe name: np:\\.\pipe\LOCALDB#3786839C\tsql\query

 

Leave a Comment

Your email address will not be published. Required fields are marked *