10
votes

I'm doing a code first Entity database

{"An exception occurred while initializing the database. See the InnerException for details."}

It was working fine, but I decided to delete the physical database to see it recreate it, but it just fails every time.

I get the error

Cannot attach the file 'C:\ASP\OdeToFood\OdeToFood\App_Data\OdeToFoodDb.mdf' as database 'OdeToFoodDb'."}

Its code first so I don't understand why its not recreating the database.

1
Have you used the migration feature? you may need to prune out your old migrations and just redo it. I find every once in a while the code gets misconstrued. - Nomad101
I haven't how is that done? - Dale Fraser
This should be very useful for you msdn.microsoft.com/en-us/data/jj591621.aspx - Nomad101
Im not doing a code migration. I have a code first model with no database, I just want it to create it again. I shouldn't have to write any code to do it. I probably just need to reinitialise something. - Dale Fraser
what I meant is you could easily use the migration feature to generate your database. I believe its one line into the console. - Nomad101

1 Answers

14
votes

So deleting the file from Visual Studio was a dumb idea.

SQL still has it registered.

Fire up MS Sql Management Tool, connect to the server in my case

(local)\v11.0

You will see the database, still exists, delete it, you will get an error. The file doesn't exist. Refresh and its gone from SQL. Now run your code again and all is good.

So delete the DB from the management tool not visual studio.