How do I undo the last Add-Migration command? - entity-framework

I have created a migration using the Add-Migration command, but I'd like to change the name of that migration. How can I undo the migration command, so that I can regenerate it using the new desired name?
Is it just a matter of deleting the generated files, or this could be a bad idea?

If you haven't used Update-Database you can just delete the migration file. If you've run the update you should roll it back using Update-Database -TargetMigration "NameOfPreviousMigration" then delete the migration file.
Reference:
http://elegantcode.com/2012/04/12/entity-framework-migrations-tips/

If you haven't executed the migration yet with Update-Database, you can run Add-Migration again with the same name (you may need to use -Force) to re-execute the scaffolding. This is noted in the output of the Add-Migration command.

Just use command
Remove-migration
It will remove last added migration and update snapshot. It will not affect database so you have to rollback db in first place.

To add to #Ben 's answer, when using the dotnet ef command variety this is the remove command you need:
dotnet ef migrations remove
Which will remove your last migration and update the model snapshot.

With EntityFrameworkCore 2.0 comes the model snapshot. You will need to run the remove migration command in order to update the model snapshot. I have read that EF Core will recognize any update and revert the snapshot for you if you manually delete the migration but this has not worked for me.
https://learn.microsoft.com/en-us/aspnet/core/data/ef-mvc/migrations
https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/dotnet#dotnet-ef-migrations-remove

Update your last perfect migration via this command :
Update-Database –TargetMigration

Just use command:
Update-Database nameMigration -context nameContext
And Then
Remove-migration -Context nameContext

Related

How to re-create initial migration on the same .cs file

As common in EF Code First I've generated an "Initial Create" migration file where is located an outdated model of my db (I'm developing the app so the model is still changing). Now I have a "new model" defined in my code and instead of creating a new migration for that I just want to update the already existing file, because it still is the initial create migration.
I've tried using this without any luck
Update-database -targetmigration $initialcreate
It returns
Unable to update database to match the current model because there are pending changes and automatic migration is disabled. Either write the pending model changes to a code-based migration or enable automatic migration. Set DbMigrationsConfiguration.AutomaticMigrationsEnabled to true to enable automatic migration.
You can use the Add-Migration command to write the pending model changes to a code-based migration.
I also tried this one, but it's always creating a new .cs file
Add-Migration InitialCreate
I'll appreaciate your help
Here you can see how I solved this.
Be careful with this approach because in my scenario I'm still in development phase so I do not need to keep the database nor the data.
If you want to overwrite the current "InitialCreate" file and re-generate the DB, follow these steps:
1) Run in Package Manager Console:
Add-Migration InitialCreate -force
2) Delete the physical database (use SSMS, T-SQL or your prefered)
3) Run in Package Manager Console:
Update-Database -TargetMigration:0 | update-database -force | update-database -force
If you do not care about the current "InitialCreate" file itself, want to create a new one and re-generate the DB, follow these steps:
1) Delete current "InitialCreate" .cs file
3) Run in Package Manager Console:
Add-Migration InitialCreate
4) Delete the physical database (use SSMS, T-SQL or your prefered)
4) Run in Package Manager Console:
Update-Database -TargetMigration:0 | update-database -force | update-database -force
My Project is Solved.
Run in Package Manager Console:
Drop-Database
Remove-Migration
Add-Migration InitialCreate
update-database
Just delete the initial migration files from the 'Migrations' folder and retry.
Try updating your database by writing Update-Database in Package Manager Console instead.

EF Code First doesn't see all migrations

In my project I have dozen+ migrations. http://screencast.com/t/CA2kZk3WCFj
But when I try to create database from scratch EF only starts from the marked migration to the bottom.
if I enter command:
update-database -targetMigration InitialCreate
this is the response:
he specified target migration 'InitialCreate' does not exist. Ensure that target migration refers to an existing migration id.
How can I resolve this issue and make EF see all of the migrations?
This can often happen if you use the Update-Database command in Release mode. Running in Debug mode seems to work ok.
I had the same problem.
In my case, the "ignored" migrations were not in the same namespace (caused by a project renamed).

What is the reverse for Update-Database (used in Package Manager Console)?

I have been changing my domain classes and executed Update-Database in the Package Manager Console, After i realized that need to add an index and need to down-grade to the previous state. what is the proper command to down-grade one step in migration?
Update-Database has a TargetMigration parameter that you can use to update to a particular migration. You can use that to upgrade or downgrade.
Update-Database –TargetMigration PreviousMigrationName
For Entity Framework Core ONLY
and if you are using it from Package Manager
Update-Database –Migration yourMigrationName
Update-Database –TargetMigration "lastcreatedMigrationName"
example:
update-database -targetmigration: "entity-logs-index"
If you want to rollback all migrations you can use (Stolen from this answer):
Update-Database -TargetMigration:0
But in EF Core it is
Update-Database -Migration:0
To continue this, if you have outstanding model changes i do believe this will fail. This can be frustrating when you already have code in place.
Update-Database –TargetMigration "targetmigrationname" -force
If you use this command and include the force flag the database will migrate to that version regardless of having outstanding model changes.

Why I can't get Entity Framework Migration to initial state?

I have deleted development database, and Migrations folder from project. When running unit test with use my project development database is recreated. I don't understand why running update-database gives me
PM> Update-Database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying code-based migrations: [201302201840012_wieleDoWielu].
Applying code-based migration: 201302201840012_wieleDoWielu.
Why migration: 201302201840012_wieleDoWielu is remembered ? How Can I delete it? Where is stored?
Best Regards
Przemysław Staniszewski
Here's a answer. Please recover migration folder and its migrations file (recover to your project). Then follow steps from my theard
http://pawel.sawicz.eu/entity-framework-reseting-migrations/
1) Update-Database -Targetmigration:0 - it's the key! first
2) Wipe out tables/migrations (you dont have to delete whole folder)
3) Add-Migration Initialise
4) Update-Database
Try these commands
Sqllocaldb.exe stop V11.0
Sqllocaldb.exe delete V11.0
This should remove the reference. You may have to comment out some of the changes before doing this and then re-instating the changes back into model before doing the migration.
I'm new at this, so hopefully I am understanding your problem, because it sounds like the same one I encountered recently.
A more permanent solution may be to add the following code to your Globel file and replace the with what you need it to be. The article for this can be found at the link below also:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<PersonContext>());
http://ilmatte.wordpress.com/2012/12/05/entity-framework-5-code-first-enabling-migrations-in-a-real-project/

Entity Framework - Start Over - Undo/Rollback All Migrations

For some reason, my migrations appear to have been jumbled/corrupted/whatever. I'm at the point where I just want to start over, so is there a way to completely undo all migrations, erase the history, and delete the migration code, so I'm back to square one?
e.g.) PM> Disable-Migrations or Rollback-Migrations
I don't want to "update" to an original migration step (i.e. something like an InitialSchema target) because I can't find it anymore.
You can rollback to any migration by using:
Update-Database -TargetMigration:"MigrationName"
If you want to rollback all migrations you can use:
Update-Database -TargetMigration:0
or equivalent:
Update-Database -TargetMigration:$InitialDatabase
In some cases you can also delete database and all migration classes.
For Entity Framework Core:
Update-Database -Migration:0
Remove-Migration
To be clear, if using LocalDb, when you want to start from scratch just delete the database via the Database Explorer and then type enable-migrations -force in the Package Manager Console. Do not delete the database via the App_Data folder or you will have the following issue.
Update-Database -Migration 0
Remove-Migration
The documentation is here: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#update-database
and here: https://learn.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#remove-migration
It is written wrong in their documentation i guess , for me i used
Update-Database -Target MigrationName