The project I am working on is using Entity Framework 4.3 and data migrations to keep the schema up to date. Over the course of the project the migrations folder has grown and now has over 600 files. This is huge. We now have a binary which is over 12MB due to all the migration meta data.
I would like to collapse all these in to one migration and start again. My concerns are:
Delete your Migrations folder. Create a new migration and generate a SQL script for it. In your database, delete all rows from the migrations history table. Insert a single row into the migrations history, to record that the first migration has already been applied, since your tables are already there.
Removing and Resetting Migrations Remove the _MigrationHistory table from the Database. Remove the individual migration files in your project's Migrations folder. Enable-Migrations in Package Manager Console. Add-migration Initial in PMC.
Entity Framework Migration. Migration. The Migrations feature enables you to change the data model and deploy your changes to production by updating the database schema without having to drop and re-create the database. It is the recommended way to evolve your application's database schema if you are using the Code First workflow.
EF Core, when executing ef migrations add, does NOT look into real database. Only your code (and DB model snapshot in Migrations folder) is used. So, you need remove migrations from your migrations folder (using dotnet ef migrations remove multiple times) and then create consolidated using ef migrations add.
In Entity Framework Core 5 migrations we are adding a migration as a delta changes between our DbContext class and existing [DbContext name]ModelSnapshot. When generating new migration a CLI Tool will generate only the differences between those two and put them in two methods: Up and Down.
Now EF will automatically take care of the migration when you change the domain classes. In Code First Migrations, you need to execute the following commands in the Package Manager Console. Enable-Migrations: Enables the migration in your project.
First: I recommend that you keep your migrations in a separate assembly so they don't have to be published with the application. It could be a simple console app that applies the migrations or a winforms GUI that generates scripts. But there's no reason for it to be deployed with the app imo.
Second: Understanding that you'd be giving up the ability to roll back to previous versions, you could just exclude-from-project all of your prior migrations then generate a new one which should then be able to create a database reflecting your current model. That would serve as your new starting point. Remember that EF doesn't always generate code to do everything you want in a migration, so you might have some hand-written migration code in other migrations you'd need to pull in.
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