EntityFrameworkCore, code-first migrations in Azure DevOps
An application that has a dependency on a SQL database needs to connect to an instance of a database that has a compatible schema. Otherwise, the application breaks. Changing the database means lots of work. Not to mention the risk associated with changing the schema itself. Plenty of reason not to release your software too often. Or is it?
When you do something often, you automate it. If you are releasing your software often, updating its database schema is a great candidate for automation. It is easily automated, too. Especially with Entity Framework and a code-first approach.
What’s code-first?
Traditionally, a DBA creates a database first. The software is developed against the database, and voila! The software works!
But you can do it the other way around, too. First, build the software (without the database in mind). Then create a compatible database schema afterward, based on the software. Not the other way around.
This article describes how to apply code-first in your project. How to create the resulting database migrations. And finally, how to update your database in an Azure DevOps pipeline. The code snippets in this articles are part of a complete, working example that you can find here.
Step 1: Create a data model
Assume we want to build an application that stores people’s names and addresses. To do that, we create a…