4

I'm new to asp.net mvc 4

Below is my connection string

<add name="MovieDBContext"
     connectionString="Data Source=(LocalDB)\v11.0;
                       Initial Catalog=Movies;
                       AttachDbFilename=|DataDirectory|\Movies.mdf;
                       Integrated Security=True" 
     providerName="System.Data.SqlClient" />

I'm getting following error when trying to access a particular control by url

Invalid value for key 'attachdbfilename'.

The error itself says it is due to a wrong connection string,but I cant find where the problem is. I'm using VisualStudio Management Studio.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Daryl
  • 339
  • 1
  • 5
  • 17
  • 1
    `AttachDbFilename=|DataDirectory|\Movies.mdf` does this file exist? – Lotok Nov 14 '13 at 11:42
  • im following this tutorial http://www.asp.net/mvc/tutorials/mvc-4/getting-started-with-aspnet-mvc4/adding-a-model How can i know wheather that file exists? – Daryl Nov 14 '13 at 11:44
  • 1
    By Default |DataDirectory| is your application folder. Possibly also a duplicate of this: http://stackoverflow.com/questions/17525905/unable-to-retrieve-metadata-mvc-application/17526947#17526947 – Lotok Nov 14 '13 at 11:46
  • No there is no such a file.so what am i supposed to do.Should i create a database manually inside that folder?But in this tutorials it doesnt say user to create a DB or something like that.. This turorial uses code first approach in Entity Framework – Daryl Nov 14 '13 at 11:56
  • What db initializer are you using (I haven't read that tutorial)? Be sure it is one that can create the DB if it doesn't exist. DropAndCreateOnChange (may not be exact name) probably won't create it from scratch. – Lotok Nov 14 '13 at 12:05

1 Answers1

6

Try changing your connection string to:

<add name="MovieDBContext"
    connectionString="Data Source=.;Initial Catalog=Movies;AttachDbFilename=|DataDirectory|\Movies.mdf;Integrated Security=True"
    providerName="System.Data.SqlClient" />

Change Data Source to : Data Source=.; or Data Source=.\SQLEXPRESS;

After that, you need to configure access right to App_Data. For Win7, On Security tab in properties Add user network Service with full right.

Found similar question:

asp.net mvc Invalid value for key 'attachdbfilename'

Or in my opinion, it might be just a typo in your connection string. Because you don't have escape character for (LocalDB)\v11.

Try writing Data Source=(LocalDB)\\v11;

Community
  • 1
  • 1
freshbm
  • 5,540
  • 5
  • 46
  • 75