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.
Related
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.
In Entity Framework 6 I'm using the Update-Database command to apply migrations. I've got three environments that I juggle (DEV, QA and PROD), and upgrade them using
Update-Database -ConnectionStringName DEV
But, now I'd like to know which migration my PROD environment is at, and which migrations willl be applied if I call Update-Database.
Is there a command for checking which migration is the latest one applied, and which will be applied if I run Update-Database?
To see which migrations have been applied to the database use the command Get-Migrations:
Get-Migrations -ConnectionStringName PROD
You can also check the contents of the table __MigrationsHistory in the right database. It contains info about all migrations applied to the database.
The next migration applied depends on the existing migration files in your project. A migration file name includes a prefix that is a timestamp, which specifies the time at which the migration file was generated (unless you used the -force parameter that might cause to reuse an existing migration file keeping its existing timestamp string). The migrations are applied according to that timestamp. So the alphabetical ordering of your migration files indicates the order in which they are applied.
A safe way to check which migration will be applied next is to run Update-Database with the -Script parameter, which generates the SQL script for the migration but does not run it. So you can see which migration would be applied if you run the real Update-Database.
Update: in Entity Framework Core this command is not available. You can still check __MigrationsHistory table contents. To generate the SQL script that would be executed without running it you can use the command Script-Migration.
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).
I am using Beta 3 of EF power tools for EF5.0 to reverse engineer an existing database.
When I select "Reverse engineer code first" from the project context menu, I get all the models and the DBContexts + mapping as expected. And all looks good.
I enabled Migrations successfully immediately after the reverse engineering process completed.
However I want to add a new property to one of the models. After adding the new property,
I run PM> Add-Migration AddMyPropertyToMyTable
a Migration file is created,
If I then try PM> Update-Database
I get an error telling me that the tables already exist.
I am following the tutorial here:> http://msdn.microsoft.com/en-us/data/jj200620
Why am I getting this error? of course the table exists, I just reverse engineered it
Am I supposed to delete the database after reverse engineering? Or in the case of a reverse engineered Db, do I have to make my changes to the actual database and just reveres engineer it again to get the desired changes in my project (so what's the point of reverse engineering in the first place?)
is there something missing from the tutorial, i.e. an extra step required to make the database updateable after model changes?
When you enabled migrations with the existing database, EF didn't add the __MigrationHistory table or initial migration (DbMigration) file.
You can add an initial migration by using the following in the package manager console:
Add-Migration Initial -IgnoreChanges
This will be an empty initial migration. Then to force EF to create the __MigrationHistory table, you can use:
update-database
This should then create the __MigrationHistory table (under System Tables)
You should now be able to make model changes, and create new migration files (or use automatic migrations by configuring it in your Configuration.cs file under the Migrations folder).
You can run these migration changes by hand by using update-database, or have the database automatically migrated to the latest migration on application startup by using the MigrateDatabaseToLatestVersion initializer.
You can set this in the app.config/web.config so that it isn't set in production for example.