1

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?

3
  • either you have to recreate it manually or force EF to initialize the database again, for example using an additional constructor. Commented Oct 23, 2015 at 17:23
  • @DevilSuichiro, does initializing the db again requires drop deleting it first? Commented Oct 23, 2015 at 17:27
  • not if you write your own initializer, however that might be a not as simple task. Commented Oct 23, 2015 at 18:23

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

1 Comment

After deleting table in db: use ctrl+r, ctrl+r to rename entity, than add-migration, in migration in Up leave createTable, in Down leave dropTable. update-database. One more time add-migration/update-database to set keys. And rename and add;update. This will save correct migrations if you need to rollback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.