You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
It would be good to have the ability to squash several migrations into a single file to help reduce the number of files in a project.
We probably want to keep track of the original list of migration names so that we can reason about this when targeting an existing database that the original migrations were applied to in their un-squashed form.
Reacted by Graeme Miller, Nathan Friend, Remigijus Grigaliunas, Pascal Honegger, Shubin Pavel, Anthon Nilsson, ajayvikas, Patrick Westerhoff, Alexey, Nicky Laurent and 427 more
Reacted by Peter Grman, unitrix0, Jimbo, Anyelo Almánzar, AliReZa Sabouri, Matteo, Mateus Viegas, Christoph Fink, Arian Kadkhoda, nmr1367 and 15 more
Reacted by Halid Cisse, Alex Rankin, draganndi, Marcin Drabek, Daniel Jonsson, Schmitt Christian, kate and Jesús Mendoza
@popcatalin81 I suspect at first, it will simpy concatenate all the operations together into one migration. In the future, it may try and simplify the operations (e.g. renaming A -> B -> C will become just A -> C)
Correct, "rewriting history" is always a bad idea. Before squashing, you'll have to revert all the migrations you want to squash, squash them, then re-apply the new one. You shouldn't do it if the migrations have been applied on any database other than your local one.
This operation would be useful while developing a new feature. You could add all the migrations locally you want, but before merging your feature, you could squash them all down into a single migration.
Reacted by Arend van Waart, Remigijus Grigaliunas, PM Extra, jonasaebersold, Ricardo Arôso Mendes Kury, Bryan Wood, Julius Jacobsohn, Oliver Brown, Fredrik Christoffer Chrislock, Lars Weber and 8 more
I was wondering if removing them all and creating an "initial migration" would be a better approach. In the end this is what I did recently. Of course the "initial migration" should be executed only on database creation. This will not only reduce the number of files in the project but it will also speed up the initial database creation if you recreate it multiple times e.g. for development and testing purposes. What do you guys think? @rowanmiller@bricelam
I'm puzzled that this issue is so inactive. How are others solving the issue of the ever growing Migrations folder?
Could there be at least some best practice described in the documentation @AndriySvyryd ?
Reacted by Dennis Roche, Dariusz Firek, Deiby Montoya, Aleksandr Pruks, Max, christiansk, Richard Ward, Aviad Pineles, Zeb Barnett, Brandon Whiteman and 11 more
This is a common problem we run into every once in a while. It's quite simple to accomplish. If your database is already up to date just delete all the migration files and truncate the dbo.__EFMigrationsHistory table. Generate a new initial create migration and you have now squashed all your migrations. You lose any comments but that's minor if you're needing to do it.
Reacted by Yury, Sander Struijk and Ryan Sorensen
Reacted by Hamed Hasani, Jeff Pang, Darkwell, Ali Torun and Paul DeVito
@aaref-sh Isn't this the same thing as the other project proposal mentioned a few comments up? #2174 (comment)
It looks yes, its similar I guess, however, I wasn't lucky enough to find this repo before you mention it, although I searched too long,
but guess what! Hurray!!, now we have 2 tools to squash our migrations, isn't that awesome?
With Ai, I asked to put in place a solution to dynamically compile those designer.cs files on demand (roslyn). In my case, I keep the last 5 ones intact and old ones are copied to an embedded text file per migration in the project. It works fine, it adds a delay of about 5s per migration for 20000 lines designer files that needs to be compiled on demand. It works for updating the database at the startup of the app or update-database or script-migration. I am not going to share the code here, but it is easily reproducible with chat gpt or others.
Reacted by ghosttie, TsengSR, Ravindu Liyanapathirana, xiety, Tibor Tóth, kate, Lure5134, Christopher True, sthg321, Jesús Mendoza and 2 more
@yepeekai 100% certain you didn't test it, because migrations are more complicated than just tossing everything into the same file.
Up/Downs must work as before, also if you have your app at different versions at different customers, they all need be upgraded first to a specific version, otherwise you'd have errors.
i.e. if you have Migrations A, B and C at customer and you squash A, B, C, D into one file as D', then deploy it, it will run on error because when you roll your squadshed migration to the customer he doesn't have D, but you have D squashed, so this migration will be missing from the customers system.
What if you need to roll back a deployment? You can't roll to a specific version after you have squashed because your "down" method would roll back everything. Just some AI slop won't help you here.
I did not squash anything, all migrations are still there and each ones can execute on demand. I did not try the down method since I don't use that, but mainly it hooks on the BuildTargetModel method in the designer.cs Up definitely works. I don't see why Down wouldn't. I am talking about the designer.cs of each migration, which in our case is a problem since it increases CI compile time by a lot. It is mainly to solve that without losing any functionality.
Activity
popcatalin81 commentedon May 14, 2015
@rowanmiller how will squash work? Merge Migrations into one or simply place them in a single file.
I think merging will be problematic when a target database has partial migrations but not all from a squash.
bricelam commentedon May 14, 2015
@popcatalin81 I suspect at first, it will simpy concatenate all the operations together into one migration. In the future, it may try and simplify the operations (e.g. renaming A -> B -> C will become just A -> C)
Correct, "rewriting history" is always a bad idea. Before squashing, you'll have to revert all the migrations you want to squash, squash them, then re-apply the new one. You shouldn't do it if the migrations have been applied on any database other than your local one.
This operation would be useful while developing a new feature. You could add all the migrations locally you want, but before merging your feature, you could squash them all down into a single migration.
TheMadKow commentedon Dec 8, 2015
👍 For this idea
markusvt commentedon Feb 3, 2016
I just wanted to suggest that idea also.. the migrations folder gets quite large quite fast if the projects develops over time
dario-hd commentedon May 3, 2017
I was wondering if removing them all and creating an "initial migration" would be a better approach. In the end this is what I did recently. Of course the "initial migration" should be executed only on database creation. This will not only reduce the number of files in the project but it will also speed up the initial database creation if you recreate it multiple times e.g. for development and testing purposes. What do you guys think? @rowanmiller @bricelam
pgrm commentedon Nov 15, 2017
I'm puzzled that this issue is so inactive. How are others solving the issue of the ever growing Migrations folder?
Could there be at least some best practice described in the documentation @AndriySvyryd ?
bricelam commentedon Nov 15, 2017
@pgrm I briefly mentioned a strategy in the Migrations docs I'm adding...
replaysMike commentedon Mar 29, 2018
This is a common problem we run into every once in a while. It's quite simple to accomplish. If your database is already up to date just delete all the migration files and truncate the dbo.__EFMigrationsHistory table. Generate a new initial create migration and you have now squashed all your migrations. You lose any comments but that's minor if you're needing to do it.
PMExtra commentedon Apr 12, 2019
@replaysMike It will lost my custom migration operations. (For example, I set a custom default value for a new field.)
127 remaining items
aaref-sh commentedon Jun 2, 2025
I'd just created a new tool for that, called "EfMigrationsSquash"
explore it in this repo
jasekiw commentedon Aug 28, 2025
@aaref-sh Isn't this the same thing as the other project proposal mentioned a few comments up? #2174 (comment)
aaref-sh commentedon Aug 29, 2025
It looks yes, its similar I guess, however, I wasn't lucky enough to find this repo before you mention it, although I searched too long,
but guess what! Hurray!!, now we have 2 tools to squash our migrations, isn't that awesome?
yepeekai commentedon May 13, 2026
With Ai, I asked to put in place a solution to dynamically compile those designer.cs files on demand (roslyn). In my case, I keep the last 5 ones intact and old ones are copied to an embedded text file per migration in the project. It works fine, it adds a delay of about 5s per migration for 20000 lines designer files that needs to be compiled on demand. It works for updating the database at the startup of the app or update-database or script-migration. I am not going to share the code here, but it is easily reproducible with chat gpt or others.
TsengSR commentedon May 13, 2026
@yepeekai 100% certain you didn't test it, because migrations are more complicated than just tossing everything into the same file.
Up/Downs must work as before, also if you have your app at different versions at different customers, they all need be upgraded first to a specific version, otherwise you'd have errors.
i.e. if you have Migrations A, B and C at customer and you squash A, B, C, D into one file as
D', then deploy it, it will run on error because when you roll your squadshed migration to the customer he doesn't have D, but you have D squashed, so this migration will be missing from the customers system.What if you need to roll back a deployment? You can't roll to a specific version after you have squashed because your "down" method would roll back everything. Just some AI slop won't help you here.
yepeekai commentedon May 13, 2026
I did not squash anything, all migrations are still there and each ones can execute on demand. I did not try the down method since I don't use that, but mainly it hooks on the BuildTargetModel method in the designer.cs Up definitely works. I don't see why Down wouldn't. I am talking about the designer.cs of each migration, which in our case is a problem since it increases CI compile time by a lot. It is mainly to solve that without losing any functionality.
mendozagit commentedon Sep 11, 2026
Roslyn is killing my machine 64GB RAM core i9 I have about 200 migrations anyone have been squashing migrations before?
mendozagit commentedon Sep 19, 2026
The assembly/project/place doesn't matter, it needs to be compiled anyways, that's the problem.