简体   繁体   中英

Is it safe to delete a .cs migration file after Remove-Migration and Update-Database

In order to learn how to use migrations in .NET Core, I created a bogus class, did an Add-Migration / Update-Database. Then I changed the bogus class and did another Add-Migration / Update-Database. I backed them out using Remove-Migration / Update-Database twice. Everything's fine and I am more comfortable with how it all works.

Now I'm left with 2 bogus migration files and corresponding designer files as well as the bogus class. Can I safely delete all of these? Is there some protocol for permanently removing them? I have checked and their entries in the _efmigrationhistory table are gone. Plus I probably have to remove reference to the class in the context file. What else will I need to get rid of?

If done well the dotnet ef migration remove command should remove the files itself. If somehow these files didn't get removed it would be safe to delete them yourself.

The purpose of Migrations is to incrementally update the database to match your new data models while preserving the existing content of the database.

You could even call it a form of version control for databases. For example, if you made a bad Migration, you can revert to the last Migration you are comfortable with.

CLI > dotnet ef database update MigrationToReturnTo
PMC > Update-Database -Migration MigrationToReturnTo

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

Please note that Entity Framework Core is notorious for having messy Migrations. I personally do not recommend removing Migrations unless you are sure you want to do a complete database reset.

If you run into issues about the database not being able to update due to records already existing, your best option is to completely delete your Migrations folder and the __EFMigrationsHistory table, and create a new initial Migration.

In my knowledge, it is safe to delete those files. If you delete the migration files, you're basicly set.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM