I am using EF migrations in my application with SQL Azure database. It was working fine until I manually dropped a table in SQL Azure. Now when I publish my application that dropped table is not created in SQL Azure. This is the error I get.
Cannot find the object "dbo.TableName" because it does not exist
or you do not have permissions.
I feel like I created some inconsistency between the database and my model.
I am using Automatic migrations.
Why did you delete the table to begin with? Do you want it to be re-created?
Before we get into the trickier solutions have you tried using the -TargetMigration option to roll back to the migration just before the one that has your table create in it? I have a feeling you will get SQL errors about trying to delete a table or index of fk that isn't there, but it's worth a shot. You can do this by using this command update-database -TargetMigration YourOldMigration
. This will roll back all the migrations that were applied after your target migration by running the commands found in the Down() methods of your migration files. If you are getting SQL errors you could try modifying the contents the Down() methods to avoid the errors. Careful. This may result in data loss. If EF warns you about this and you don't care. Try adding -Force to the end your command.
Alternatively and additionally....
Your migrations aren't just calculated by comparing your db scheme to your dbcontext/models. If you open your sql azure database in sql server management studio you should see a table called __MigrationHistory. It stores all the migrations that have been applied to your db through automatic migrations.
Please read all the way through before starting. There are some factors that you will want to consider before jumping in. Assuming you didn't already manipulate this you should be able to find a row for the change set that originally created your table. Delete that row. Now EF automatic migrations will think that that change has not been applied to your DB yet. If you run update-database it should try and re-run it.
If you had other changes in that migration file it will try and re-run those too. This can cause all sorts of sql errors. You will probably want to manually roll back all the changes that were also part of that migration. Worried about data loss? Try copying data into a manually created table to store until after you are finished. After you get the migration to work you can copy the data back into your new tables/columns.
Double alternative. If you aren't too far into development and you don't have too many concerns about data loss it may just be easier to drop the entire database and let automatic migrations re-create it from scratch.
Hope that gets you there
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With