Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to force EF code first to recreate databases?

I had a bunch of tables Code First created.

Then in SQL i deleted one table so that i could inevitably ask this question on stack.

Upon using update-database in package management console I get:

Cannot find the object "dbo.ContractParents" because it does not exist or you do not have permissions.

What is the best way to recreate my table?

I've read up about context.Database.CreateIfNotExists();

I put it in my seed function, but nothing doing.

Thanks!

like image 862
Pinch Avatar asked Oct 01 '13 14:10

Pinch


People also ask

How do you use code first when an existing database schema?

To use code-first for an existing database, right click on your project in Visual Studio -> Add -> New Item.. Select ADO.NET Entity Data Model in the Add New Item dialog box and specify the model name (this will be a context class name) and click on Add.

How do I use database first approach in Entity Framework?

Step 1 − Let's create a new console project with DatabaseFirstDemo name. Step 2 − To create the model, first right-click on your console project in solution explorer and select Add → New Items… Step 3 − Select ADO.NET Entity Data Model from middle pane and enter name DatabaseFirstModel in the Name field.


3 Answers

To explain what happens with your update-database command and why the context.Database.CreateIfNotExists() method in the seed did not work:

When you run the update-database command it first looks at your connection string to see if the database is there. If it is it looks at the migration history table and checks that against what is in you DbContext class. If it sees that there are tables missing or changes it will attempt to update the database. The Seed method is not called until after that is done, so that is why that did not work.

While developing using EF-Code First I usually approach the problem in a few different ways depending on how large my database is. I usually went the route of deleting all the tables (including the migration history table) and then running the update-database command again. Works fine, just really time consuming if you have a lot of tables with a lot of FK constraints on it.

I finally became tired of it and found these scripts to make the dropping of tables exponentially faster. I went to this because I was running my app on Azure. When I am running it on my local machine, I would just delete the whole database and make a brand new database with the same name.

Elegant solution? No. Does it work? More or less...

like image 141
ledgeJumper Avatar answered Oct 20 '22 14:10

ledgeJumper


For yet another cheesy option...

Right click on your db in server explorer, and hit delete Then you can do

Enable-Migrations -EnableAutomaticMigrations -Force

Update-Database -Force

Dirty update, clean result :)

like image 37
Steve Avatar answered Oct 20 '22 13:10

Steve


For a quick and dirty approach, that'll get you home for dinner on time along with lots of dataloss (i'm still in beta using test data)

drop the dbo.__MigrationHistory system table

along with all of your other tables.

Back up your data first!

update-database -verbose (you may need some sauce with your spaghetti)

I am not impressed, but it works.

Hopefully someone will come up with a better answer in the future.

It would help to really understand migrations better.

like image 4
Pinch Avatar answered Oct 20 '22 14:10

Pinch