10

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.

6
  • 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. Commented May 5, 2013 at 4:34
  • I haven't how is that done? Commented May 5, 2013 at 4:38
  • This should be very useful for you msdn.microsoft.com/en-us/data/jj591621.aspx Commented May 5, 2013 at 4:39
  • 1
    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. Commented May 5, 2013 at 4:43
  • what I meant is you could easily use the migration feature to generate your database. I believe its one line into the console. Commented May 5, 2013 at 4:45

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

3 Comments

To save a download, you can also do this from within Visual Studio via the "SQL Server Object Explorer".
Here it should be: (localdb)\v11.0 (local-->localdb)
I also had the same problem by deleting mdf from VS which was working fine previously. But this solution did not help me as I could not see my database or tables in server management studio. My tables was not in my local data base and I am not able to locate it through server management studio to delete it as you said.

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.