Unable to attach file '{0}' as database '{1}'. Code first. Local SQL EXPRESS instance. VS 2010 SP1. Windows XP

I am using Code First approach. I created a local sql express database "Database.mdf" in Visual Studio 2010 SP1 using the MVC 4 pattern. When I try to run my project on my PC, I get the error:

Allow CREATE DATABASE on the database. Unable to connect file "... Database.mdf" as database file "Database".

What I have tried:

  • delete my database using SQL SERVER Object explorer (no results, same error)

  • change connection string (no result, same error)

  • googled on the internet. However, I've seen examples where people are using real SQL servers rather than a local database.

My connection string:

connectionString="Data Source=.\SQLEXPRESS; InitialCatalog=Database; AttachDBFilename=|DataDirectory|\Database.mdf; IntegratedSecurity=true;"  

      

Please note that I am using a local database and not a real SQL SERVER and my Visual Studio is 2010.

I am trying to run this code:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

      

How to solve it? Any help would be greatly appreciated!

+3


source to share


2 answers


  • Create a new blank database (Graphical) in your SQL Express.
  • Change the connection string "InitialCatalog" to something else: InitialCatalog = Database1 ;.
  • Run the project and use one query.


+2


source


It is not a LocalDb connection string. The LocalDb connection string looks like this:



connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True" 

      

+1


source







All Articles