11 Handling database migrations

 

This chapter covers

  • Using EF Core’s migration to update a database
  • Building a DbContext from an existing database
  • Changing a database by using SQL command scripts
  • Applying updates to your production database

This chapter covers the three ways of changing the structure of a database. The structure of the database is called the database schema—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 might not be immediately apparent; for example, moving data from one entity class to another entity class can cause loss of data when applying a database schema change. Getting a database change wrong on a database that has live data in it is a scary problem.

I’ve split the chapter into two distinct parts. Part 1 describes all the ways to update a database’s schema when working with EF Core. Part 2 presents the issues around a database schema change, starting with simple changes and working up to the much more complex situations requiring real care to ensure that data isn’t lost.

11.1 Part 1—EF Core methods to change the database schema

 
 
 

11.1.1 A view of what databases need updating

 
 

11.2 Code-first: using EF Core’s migrations

 
 
 
 

11.2.1 Stage 1: creating a migration—building the code for migration

 
 
 
 

11.2.2 Stage 2: applying migrations—updating a database schema

 
 

11.2.3 Undoing a migration—Remove-Migration or update command

 
 
 
 

11.3 Database-first: creating a DbContext from a database

 
 
 

11.3.1 How to alter or edit the output from the scaffold command

 
 

11.3.2 The limitations of the reverse-engineering feature

 
 
 

11.4 SQL-first: using SQL change scripts to change the schema

 
 
 

11.4.1 Using an SQL comparison tool to build an SQL change script

 

11.4.2 Using EfSchemaCompare to check your SQL matches EF Core’s model

 
 
 

11.5 Part 2—Issues around a database schema change

 
 
 

11.5.1 Applying nonbreaking changes while the current app is running

 
 
 

11.5.2 Applying breaking database changes by stopping the application

 
 
 
 

11.5.3 Handling breaking database changes when you can’t stop the app

 
 
 

Summary

 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage