January 28, 2021
Working with multiple environments can make it hard to keep databases in sync with code. Use a database project to make it managable.
To avoid one-off scripts or manual steps in deploying database schema changes, a database project can be helpful.
For code-first solutions, migrations are helpful for making sure your local database matches your code.
A manual schema-compare step keeps the database project in sync with your local database.
We then compile the database project to a format the Azure Pipelines understand, and automatically update DBs as part of deployments to keep data in sync.
If a database migration can't happen automatically, the deployment fails and is blocked.
Option a. Making DB updates (Code-first Migrations using .net core)
add-migration asfasdf
to create a migration to get your DB to match your local C# entitiesupdate-database
to update your DB.sql
files to match your local envor Option b. Making DB updates (DB-first using .net core)
.sql
files to match your local envCreate a database project in VS
Perform an initial schema compare - LocalToProject
or DevelopmentToProject
Check in your changes to git
Add a build step to your azure-pipelines.yml
file after your other build steps
- task: VSBuild@1
displayName: "Build database project"
inputs:
solution: "**/ProjectName.Database.sqlproj"
msbuildArgs: '/p:OutputPath="$(Build.ArtifactStagingDirectory)\drop-dacpac"'
configuration: "$(BuildConfiguration)"
Add a deployment step to your release (before deploying your app)
Azure SQL Database deployment
Azure Resource Manager
$(AzureSubscription)
Connection String
$(ConnectionString)
$(System.DefaultWorkingDirectory)\**\*Database.dacpac
Further reading...