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?
Therefore, although you can safely delete old Migration files, you potentially lose the ability to return to a previous schema for your database.
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.
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.
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:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With