Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core: Is it safe to delete Migration.Designer.cs if we will never Revert a migration?

We have a database schema with ~200 tables. Model snapshot (Migration.Designer.cs) which is created for each migration is ~20K lines. So, having quite a number of migrations really slows down our build on CI (with ~30 migrations building a solution takes 6 minutes with migrations or 4 minutes without them).

So, to the question: is it safe to delete model snapshots for old migrations (that we know we will never Revert)? Are model snapshots used for anything else except Revert-Migration?

like image 714
Shaddix Avatar asked Nov 01 '17 05:11

Shaddix


People also ask

Is it safe to delete migration file?

Therefore, although you can safely delete old Migration files, you potentially lose the ability to return to a previous schema for your database.

What does remove migration do?

Remove a migrationTo remove the last migration, use this command. After removing the migration, you can make the additional model changes and add it again. Avoid removing any migrations which have already been applied to production databases.

How do I revert my EF core migration?

Reverting a Migration In this case, use the update-database <migration name> command to revert the database to the specified previous migration snapshot. > dotnet ef database update MyFirstMigration.


1 Answers

Are model snapshots used for anything else except Revert-Migration?

Yes. There are a few edge cases where it's needed. On SQL Server, those cases are:

  • AlterColumn when the column is narrowed or the computed expression is changed and the indexes need to be rebuilt
  • CreateIndex on a memory-optimized table when the index is unique and references nullable columns

So most of the time it's probably safe to delete, but please test that your migrations still work after doing so.


The .Designer.cs file contains a partial class, with 2 attributes:

[DbContext...
[Migration...

Don't forget to copy these attributes to the class containing your migration code (the Up and Down methods of the same partial class). EF uses these attributes to determine which migrations are in the assembly.

After removing the .Designer.cs files from our project, dbContext.Database.GetPendingMigrations().Count() returned 0.

We solved this problem by adding these attributes.

like image 122
bricelam Avatar answered Oct 05 '22 08:10

bricelam