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

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.

Related

EF Core Migrations not being recognized

after I execute the add-migration in the PMC the migration class is created with all the correct changes need in the Up method.
When I run the update-database command in the PMC I get the error
System.InvalidOperationException: The migration '***MigrationName***' was not found.
I have never seen this before and the file it says is not there clearly exist.
If I execute the remove-migrations command the newly created migration class is not removed, I get an error stating that the previous migration has already been applied.
It appears that all new migrations created with add-migrations are not recognized via the update-database or remove-migration commands
One of the reasons for this is that dbcontext is not in startup project and it is inside another project. In this case PM may not be able to find routes to complete migrations. We can use dotnet with explicit dbcontext and proj name where dbcontext is:
dotnet ef database update --context YourAppContext --project YourProj
We just had the same issue. The problem was that, we excluded the Migrations folder from the solution in Visual Studio.
Project ⇒ Show all files
Right Click on "Migrations"
» Include in project
The above steps resolved it for us.

Entity Framework Migrations: Pending migration after "update-database"

I'm using entity framework version 6.1.3 and .NET Framework 4.0.
I'm trying to use Migrations from Code-First from existing Database.
I'm having a trouble that whenever I use the Update-Database it makes the changes from my migration, and changes my database, but it keeps the migration as "Pending" and when I try to add another migration with Add-Migration, it says I have pending migration.
Example of the problem:
Run the Enable-Migrations command from the Package Manager Console.
Run the Add-Migration first command to create an initial migration.
Remove all the code in Up() method for the initial migration, because I don't want to create the tables that already exists.
Run the Update-Database command to apply the initial migration to my database (it runs successfully).
Make changes to my code-first model (add column to existing table).
Run the Add-Migration second command to create a new migration.
When I run step 6, I get the following error:
Unable to generate an explicit migration because the following explicit migrations are pending: [201601111013546_first]. Apply the pending explicit migrations before attempting to generate a new explicit migration.
If I delete the migration "[201601111013546_first]" from my Migrations folder, I can run step 6, and use the Update-Database, and it successfully changes my database, adding the column I wanted, but if I change my model again and use Add-Migration third, it says the same error, that the second migration is still pending.
Also, if instead of steps 5 and 6, I just run Update-Database again, it runs the same script returning the obvious error of already having a 'MigrationId' in the table dbo."__MigrationHistory":
ERROR: 23505: duplicate key value violates unique constraint "PK_dbo.__MigrationHistory"
I already tried:
Create a new project (ConsoleApplication) and made the same steps getting the same errors.
Delete entire Migration folder and run Enable-Migrations again.
Verify the namespace of the Migration and it's .Designer file.
I have only one connectionString but still tried the commands with -ConnectionStringName.
Any suggestions?
Thanks.

How do I undo the last Add-Migration command?

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

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