25
votes

i have somehow gotten my EF5 project into a state where I can't proceed.

When I do an 'update-database' i get:

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.

ok, fine, so i try to 'add-migration', and i get:

Unable to generate an explicit migration because the following explicit migrations are pending: [ ]. Apply the pending explicit migrations before attempting to generate a new explicit migration.

20 GOTO 10 ??

what am i supposed to do at this point? (beyond switching to NHibernate?)

7
If you have any migrations at all, even your initial ones, try commenting out the code and running the project. Sometimes this gets past this error.Charlie Brown
@Davy8 Can you do "Update-Database -Force"?Kittoes0124
@Kittoes I tried that but no dice. Still had the same error. I also tried -Force on the add-migration without luck. The only way I got it to work was the answer I posted which is messy but worked. Would love to hear about less ugly solutions.Davy8
Code first is good in theory, in real life, Database first is better approach. You have full control over DB, and if for any reason, bug in Code first results in loss of data in your production server, will be you responsible. Problem with code first is, you cannot preview changes, nor you can prevent harmful changes.Akash Kava
Its unfortunate this problem has not been fixed and probably never will be. EF is a great product. This just kills it.Sam

7 Answers

7
votes

What worked for me was:

  1. Reverting all my changes (making a backup first) back to a known good state.
  2. Doing add-migration DummyMigration. That produced some spurious changes that I commented
  3. Called update-database to add the migration + metadata to the [__MigrationHistory] table.
  4. Make the changes I needed (from the backup of the code I took earlier).
  5. Do the normal add-migration/update-database.

Not ideal, and would be cool to see if there's a better solution, but that worked for me.

2
votes

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.


(also posted this as an answer here because both questions are so similar)

1
votes

I changed the following value in my Configuration.cs class from false to true.

AutomaticMigrationsEnabled = true; 

In App_Data folder I renamed my project's database - the file with .mdf ending (so a new one will be created), and in Package Manager Console I entered the following command:

update-database

after which the pending migrations ran smoothly.

Note this worked for me, but I'm not 100% if this is the best practice. In any case this Entity Framework Code First guide for migrations says:

"If you get an error that indicates a table already exists and can't be created, it is probably because you ran the application after you deleted the database and before you executed update-database. In that case, delete the Movies.mdf file again and retry the update-database command. If you still get an error, delete the migrations folder.."

Also about AutomaticMigrationsEnabled = true; this MSDN article, Data Points : A Code First Migrations Mystery: Solved tells "Migrations can run automatically, meaning that model changes will be discovered and migrations corresponding to changes will be created and executed on the database. All of this happens at run time during database initialization. Automatic migrations are handy for simple apps, but you have very little control over them and I typically don’t recommend enabling them. I was happy when Code First switched the default to false."

0
votes

I did mistake in creation database

    using System.ComponentModel.DataAnnotations;
    using System.Globalization;
    namespace ProductsManager.Models
    {
        public class Product
        {
            public int ProductId { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
            public string Production { get; set; }
            public string Size { get; set; }
            public decimal<--- Price { get; set; }
            public string Barcode { get; set; }
        }
    }

after add-migration Initial I realized and changed code to public int Price { get; set; } did same add-migration DummyMigration and its created in migration folder 080372472_dummyMigration

namespace IdetityBeta.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class DummyMigration : DbMigration
    {
        public override void Up()
        {
            AlterColumn("dbo.Products", "Price", c => c.String());
        }

        public override void Down()
        {
            AlterColumn("dbo.Products", "Price", c => c.Decimal(nullable:             false, precision:              18, scale: 2));
        }
    }
}

So then update-database and problem was solved

0
votes

I got around this by

  • Run "Update-Database -Script -Force"
    • note the last explicit migration attempted before the error "Unable to update database..."
  • Run "Update-Database -Script -TargetMigration [lastmigration]" using the last migration noted from the previous attempt

I could then run in the script and add a new migration. This is on EF6 and Nuget 2.8 - it may not have worked when the question was posted.

0
votes
  1. Update-Database –TargetMigration <second_last_migration>
  2. Add-Migration <full_name_including_timestamp_of_last_migration>

    You need to include the timestamp so that migrations knows you want to edit the existing migration rather than scaffolding a new one. This will update the metadata for the last migration to match the current model.

  3. Update-Database

Source https://docs.microsoft.com/en-gb/ef/ef6/modeling/code-first/migrations/teams#resolving-the-merge-conflict

0
votes

This can happen when you are trying to merge with migration which has the same base as your migration because of which there is model difference in one of migration schema even though that table exists

To solve this problem what you can do is create merge migration by command

Add-Migration -IgnoreChanges

followed by migration name

This creates an empty migration with the current model as a snapshot. Thus this will solve your model difference problem and your tables and models will be in sync