EF Code First doesn't see all migrations - entity-framework

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).

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.

EF migrate.exe does not see my code-based migration files

We have a situation, where a team of developers started working on a project with EF code first and originally the automatic migrations were enabled. Now we need to sort out the mess, turn off automatic migrations, reset everyone's migrations history and 'start anew', forcing all developers to use manual, code-based migrations from now on.
I have been following this answer to clear the migrations history (which worked so far):
EF - Moving from AutomaticMigrations to Manual Migrations
In the meantime we would like to automatise the application of the code-based migrations in the deployment process.
For that, I have been playing around with migrate.exe as described here:
http://msdn.microsoft.com/en-gb/data/jj618307.aspx
We have a DAL project that hold all the data objects and deals with migration.
I have copied migration.exe from /packages/EntityFramework.5.0.0 to DAL/bin/Debug.
I have updated my DB to the latest, then cleared my migrations history table.
I changed a StringLength attribute in one of my models and run add-migration test.
Now, I have exactly 2 code-based migrations in my DAL/Migrations folder: one for the initial create, and one for modifying the column that I've changed. (they both have their .resx and .Designer.cs brothers)
I am trying to run migrate.exe to apply the last migration (test) like this:
migrate.exe MyApp.DAL.dll /startupConfigurationFile="..\..\app.config"
It runs, then it prints
No pending code-based migrations.
And then fails, because it wants to run an automatic migrations, and fails, because it is turned off. (which is ok, 'cause I don't want automatic migrations)
For some reason migrate.exe does not see my code-based migration files.
What am I doing wrong?
(If I try to apply the same migration from PM console, that works)

EF Add-Migration indicates "No pending explicit migrations" but Update-Database complains "..there are pending changes"

I frequently encounter the following situation with Entity Framework code first migrations. I add some migrations and update the database with them successfully. When later I wish to add a new migration, the Add-Migration command complains that its "Unable to generate an explicit migration because the following explicit migrations are pending...". I.e., it seems to forget that its already applied those migrations previously and therefore they shouldn't be now considered as pending. If I try to workaround this issue, Update-Database indicates "Unable to update database to match the current model because there are [new] pending changes...". I don't understand why EF migrations getting out of sync since I'm only making changes to the model.
For me the issue was that we had renamed the namespace of the migration 2014123456_Initial.cs.
But VS had not regenerated the namespace in its associated 2014123456_Initial.Designer.cs.
Once the Designer.cs was changed to use the same namespace, it all started working again.
I was having the same issue and the solution was:
Clean solution;
Build the project which has migrations enabled (just to make sure);
Build the Solution again;
This is pretty much equivalent of using the Rebuild Solution option in the Build menu.
I had the exact same problem and could not resolve it.
To add to IamDOM solution ALSO add an empty migration and update database.
For example:
Add-Migration Test
Update-Database
It worked for me few times now.
I found that VS had got confused somehow and not actually added the migrations to the .csproj. Closing, re-opening VS then re-including the migration in the project and saving-all worked.

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