Cannot attach the file ‘{0}' as database '

2019-07-13 00:00发布

问题:

I've been using Code First approach. I created a sql express local database "Database.mdf" in Visual Studio 2010 SP1 using MVC 4 template. When I try to run my project at my PC, I get an error:

CREATE DATABASE permission denied in database master. Cannot attach the file "...Database.mdf" as database file "Database".

What I've tried:

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

  • change connection string (no result, the same error)

  • googled in the Internet. However, I've met examples where people use real SQL servers, not the local DB.

My connection string is:

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

Please, pay attention that I use local database, not the real SQL SERVER and my Visual Studio is 2010.

I try to run this code:

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

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

回答1:

  1. Create a new empty Database (Graphical) in your SQL Express.
  2. Change your connection string "InitialCatalog" to something else like : InitialCatalog=Database1;.
  3. Run your project and use one query.


回答2:

That is not a LocalDb connection string. A LocalDb connection string looks like this:

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