user3378165
user3378165

Reputation: 6896

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?

Upvotes: 0

Views: 7127

Answers (2)

behza_d
behza_d

Reputation: 25

Firstly try these please :

  • 1- clean the project up-root solution
  • 2- close the visual studio
  • 3- reopen vs and open the solution
  • 4- build it.
  • 5- totally remove the migrations folder under the entities folder
  • 6- remove the table _projectnameMigrations from the DB
  • 7- open the PMC in Vs
  • 8- Enable-Migrations
  • 9- Add-Migrations mig_Myinit
  • 10- (!Important) clear the up method and make it empty
  • 11- update-database
  • 12- done

Your next migrations... point is that if you remove a table manually from the database Entity Framework is not able to recognize it and by default it is accessible so when you are using EF you have to do any database-related tasks from the pmc.

Upvotes: -2

kkakkurt
kkakkurt

Reputation: 2800

  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.

Upvotes: 3

Related Questions