3

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!

StepUp
  • 36,391
  • 15
  • 88
  • 148

2 Answers2

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.
xwpedram
  • 467
  • 1
  • 8
  • 20
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" 
Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • @StepUp - you also need the Initial Catalog statement, which I didn't include. Regardless, you're not using localdb unless you use the (localdb)\v11.0 datasource, otherwise you're just using sql express as standard sql server. – Erik Funkenbusch Dec 14 '14 at 04:08
  • If I write connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True", I get an error "Invalid key value: AttachDBFilename". However, ".\sqlexpress" works perfectly. – StepUp Dec 14 '14 at 13:19
  • @StepUp - as I said, you're not using localdb when you do that. If that's fine, and you don't care that you need to have SQL Server Express installed where it's going to be used, then ok. The whole point of localdb is so that you don't need an instance of Sql Express. – Erik Funkenbusch Dec 15 '14 at 04:14
  • I have not installed SQL Server Express. SQL Server express is internal part of VS 2010. If I write your connstring: connectionString="Data Source=(LocalDB)\v11.0;...", then I catch an exception. If I write "connstring=/.sqlexpress" , it works okay. However, WebSecurity cannot create tables in my internal DB of my application. It throws exception - Cannot attach the file ‘{0}' as database '{1}' – StepUp Dec 18 '14 at 14:30
  • Localdb does not come with VS 2010. See the v11? That indicates SQL Server v11 which is SQL Server 2012. In fact, VS2010 doesn't support LocalDb out of the box, there is a walkthrough here though. http://blogs.msdn.com/b/sqlexpress/archive/2011/11/28/using-localdb-in-visual-studio-2010.aspx – Erik Funkenbusch Dec 18 '14 at 19:14
  • I've seen this article and I tried conn string "localdb/v11.0", but it does not work and never databases are shown. If I write ".\sqlexpress", it is possible to see local databases. – StepUp Dec 19 '14 at 00:07
  • @StepUp - I give up, you seem completely unable to understand English. – Erik Funkenbusch Dec 19 '14 at 04:40
  • I am not sure that I use correct name for local database. I just clicked by right button of mouse at the project - Add->Add Element->Data->Database SQL Server – StepUp Dec 19 '14 at 07:18