Majid Basirati
Majid Basirati

Reputation: 2855

ALTER TABLE DROP COLUMN failed because column 'xxx' does not exist in table 'yyy'

I make a mistake. I delete some row in table __MigrationHistory in my database.
and now when I run my project I force this error:

ALTER TABLE DROP COLUMN failed because column 'xxx' does not exist in table 'yyy'.

How can I solved this problem?

Upvotes: 6

Views: 20005

Answers (5)

joker
joker

Reputation: 56

I got this one using EF Core 3.1.1 while I was trying to add a new field to a table.

I didn't delete entries in the MigrationHistory table but I reverted the last migration, removed the generated migration files and generated them again with 'add migration'. Then I noticed the new Up and Down methods of the migrations where using AlterColumn instead of AddColumn and RemoveColumn. I got the code from the previous Up and Down methods and that solved it.

Upvotes: 0

AlwaysLearning
AlwaysLearning

Reputation: 45

Here is more details since I had the same issue and this Question was help full: For whatever reason if you get this error: ALTER TABLE DROP COLUMN failed because column 'xxx' does not exist in table 'yyy'. Do this: manually update the table in the database.

  1. Go to Server Explorer (2)
  2. Tables
  3. Double click 'yyy'
  4. Add: new column of 'xxx'
  5. In the top left of Visual S under "Any CPU" there is an option "UPDATE". This will update the table.
  6. Now go to that table model under Models 'yyy.cs' Add: public string 'xxx' {get; set;}
  7. Now in PM ( tools --> Neu get package manger --> PM)
  8. Do either update-migration or rollback it is up to you.

Upvotes: 1

Majid Basirati
Majid Basirati

Reputation: 2855

OK, finally I solved my problem.
I added field 'xxx' to table 'yyy' manually in my database.

Upvotes: 3

Steve Greene
Steve Greene

Reputation: 12304

Add a new migration to re-synchronize:

add-migration MissingXXX -IgnoreChanges // tells EF to just take a snapshot
update-database

Now you are back in sync and can continue with future model changes.

Upvotes: 11

ook4mi
ook4mi

Reputation: 1

Why not altering your table to recreat the missing column ? And then try to drop your table again :)

Upvotes: 0

Related Questions