usefulBee
usefulBee

Reputation: 9702

Deleted table in database; how to recreate it using Code First

After deleting table X in database, issued update-database command in PackageManagerConsole, and now I get the following error: "Cannot find the object "dbo.X" because it does not exist or you do not have permissions."

Considering this scenario, how to force CodeFirst to create the table from scratch?

Upvotes: 1

Views: 3394

Answers (1)

usefulBee
usefulBee

Reputation: 9702

These are the work around steps that deals with this scenario:

  1. delete table from db
  2. delete all related stored procedures if any
  3. create a new imitation entity/table class with a different/distinct name (ex. BlahBlah)
  4. create any configs needed for the new entity inside OnModelCreating method
  5. in the PackageConsoleManager, issue an add-migration command
  6. open the migration file and replace all BlahBlah words with the old table name
  7. Comment or delete the old table class
  8. repeat 7 for old configs in OnModelCreating
  9. rename the new table class with the old table class name
  10. repeat 8 for the new table configs in OnModelCreating
  11. in the PackageConsoleManager, issue an add-migration command again
  12. if it creates a new migration, replace its content with the old migration created in step 6
  13. in the PackageConsoleManager, issue update-database command

Upvotes: 3

Related Questions