Cannot attach the file ‘{0}' as database '{1}'. Code first. Local SQL EXPRESS Instance. VS 2010 SP1. Windows XP

荒凉一梦 提交于 2019-12-10 20:43:41

问题


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:


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" 



回答2:


  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.


来源:https://stackoverflow.com/questions/27440143/cannot-attach-the-file-0-as-database-1-code-first-local-sql-express-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!