9 Handling database migrations
This chapter covers
- The different ways you create commands to update a database’s structure
- The three starting points from which you create database structure changes
- How to detect and fix database structure changes that would lose data
- How the characteristics of your application affect the way you apply a database change
This chapter covers the different ways of changing the structure of a database, referred to as migrating a database. The structure of the database is called the database schema—this consists of the tables, columns, constraints, and so on that make up a database. Creating and updating a database schema can seem simple because EF Core provides a method called Migrate to do it all for you: you create your entity classes and add a bit of configuration, and EF Core builds you a nice, shiny database.
The problem is that EF Core’s Migrate method hides a whole series of database migration issues that aren’t immediately obvious. For instance, renaming a property in an entity class will, by default, cause that property’s database column to be deleted, along with any data it had! So, in this chapter, as well as detailing how to build and apply database migrations, I cover the key issues that you must consider when updating a database. No one wants to be the person that breaks your ‘live’ database.