asp.net-mvcentity-frameworkasp.net-identityidentity

Errors on Add-Migration


A few weeks ago I enabled migration on my project and added a column 'ClientID' to the AspNetUsers table.

Now I'm trying to add another column: "Name".

I ran this command in the Package Manager Console:

PM> Add-Migration "Name"

And I got the following error:

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

I ran an update command:

PM> Update-Database

But:

There is already an object named 'AspNetRoles' in the database.

I researched online and found this solution:

PM> Add-Migration InitialMigrations -IgnoreChanges

But then I got the first error again:

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

How can I solve this problem?


Solution

    1. Remove the Migrations folder in your project

    2. Delete the _MigrationHistory table from your database.

    3. Run the Enable-Migrations command from your Package Manager Console.
    4. Run the Add-Migration Init command to create a migration.
    5. Remove all of the code lines in Up() function for the initial migration. (To create an empty migration and solve the errors that you are facing.)
    6. Run the Update-Database command to apply your empty migration.
    7. Make changes to your code first model for preparing to adding your real migration.
    8. Run the Add-Migration InitNew command to create a new migration.
    9. Run the Update-Database command to apply the changes.