Cannot attach file as database in multiple projects

84 views Asked by At

I have two projects which are targeting the same database. One of them is an MVC web application and the other is a Web API project. However, when I initialize an instance of the context in any of the projects I get the following exception:

Cannot attach the file 'myRoute/MyDatabase.mdf' as database 'MyDatabase'.

They way I have solved this is by:

  1. Erasing my database from the SQL server object explorer (as stated in EF5: Cannot attach the file ‘{0}' as database '{1}')
  2. Running code first migrations once again.

However, if I run the code first update while my startup project is the web application, the web services generate the same exception. If I run the code first update while my startup project is the web service, then the web application has the same issue.

What happens?

1

There are 1 answers

0
AJB On

For what its worth I had this problem and apparently solved it by adding an explicit reference to the database in the MVC Web.config file EXCLUDING the AttachDbFilename clause: