Search code examples
entity-framework-coredatabase-migrationentity-framework-migrations

Entity Framework: Slow migrations with large volumes of data


We are using Entity Framework Core in our data layer and have found that migrations are very slow when doing a large number of operations on tables with a great deal of data (millions of rows).

For instance, at the moment we're tweaking the sizes of our string (varchar) columns in the whole database. With every single column length change, EF will drop all table indexes and constraints and recreated them. For example, if we had 1 index and 10 columns that changed, EF will drop and recreate the index 10 times, rather than simply dropping the index once, changing the columns, and recreating the index again.

On some of our tables, a single column change takes over 30 minutes to do, and we have in the order of 200 column changes we'd like to make.

Is it possible to change this behaviour to drop all indexes at the start of the migration and recreate them at the end?


Solution

  • We ended up creating two migrations to solve this problem.

    First, we would hide all the index definitions in our DbContext class. We then create a migration, which will naturally drop all the indexes.

    Second, we make the changes to the data structures and uncomment the index definitions. The second migration will change the structure and add the indexes again.