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/
Related
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.
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).
In my application I enable Code First Migrations with some migrations, Also I use SQL Server Compact for integration test.
When I run my tests, Entity Framework create an empty database and tries to run migration on that empty database and thrown The specified table does not exist.
Based on this report I think usage of Migration in Entity Framework 6 has changed.
I test all Database Initializer with Context.Database.Create(); but in all case tabale's never created.
I don't know that this is EntityFramework's bug or not, but when I made rename the namespace of Migration Configuration class from default (Projectname/Migrations) to any none default name, migration works well.
Context.Database.Create() will not execute migrations! It only creates empty db. To Update database from code to latest version you need to use DbMigrator.Update method:
var migrator = new DbMigrator(new MyMigrationsConfiguration());
migrator.Update();
Alternatively you might use MigrateDatabaseToLatestVersion
Database.SetInitializer(new MigrateDatabaseToLatestVersion<BlogContext, Configuration>());
It is described in details here: http://msdn.microsoft.com/en-us/data/jj591621.aspx#initializer
In case someone still struggles to fix the issue.
The code that follows works for me: add-migration MyFirstMigration
Meanwhile add-migration "MyFirstMigration" with the migration name ramped in quote doesn't work.
There may be previous migration files which the ide may be referring to mostly likely due to caching.
Drop backup and drop target database if it exists, and drop the migration folder.
Now add the migration and you will be good to go.
It does happens when adding model and running add-migration command.
Here is the simplest cause of this issue:
Add a newly added model property into IdentityDbContex class.
Here are the steps:
create model
add property into IdentityDbContex class
run add-migration
update-database
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.
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