11

I am currently reading Manning's "ASP.NET MVC 4 in Action" book and trying to get the first example to work.

Within my test application, I built a simple Model and created some Views. I then imported "SQL Server Compact" using NuGet.

When I finally try to run the application I get the following error:

Invalid value for key 'attachdbfilename'

This occurs on every interaction with the Database (SELECT or other CRUD operations) I am running. Any ideas?

Dr1Ku
  • 2,875
  • 3
  • 47
  • 56
Bick
  • 17,833
  • 52
  • 146
  • 251

9 Answers9

11

Though, I am bit late to respond to this question but I was facing the same issue and I resolved it by modifying the connection string like below

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

Hope this would be helpful for others too.

Zerotoinfinity
  • 6,290
  • 32
  • 130
  • 206
3

I tried finding a solution for this particular error without success.

Fixed it in the end by updating the .Net Framework 4 to 4.0.2. Patch and details can be found Here is the link

Hope it helps

Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30
Teto
  • 800
  • 6
  • 15
2

I think the problems here is string in web.config is not correct According to your set up sql, the string will work if you set something similar like this

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

Data Source=.\SQLEXPRESS; --> sometime, it will be Data Source=.; after that, you need config right to access FOlder App_Data If u test on Window 7, right click folder. property Security tab -> add user network Service with full right

Wolf
  • 6,361
  • 2
  • 28
  • 25
  • 1
    It worked for me by changing to `data source=localhost\SQLEXPRESS;` and adding the `NETWORK SERVICE` user with full control under the security tab of the App_Data folder's properties. Thanks! – user1477388 Sep 20 '13 at 15:08
1

Go to section of Web.config and modify the section to the following to get SQLServerCE4 to be used:

<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlCeConnectionFactory, EntityFramework">
<parameters>
<parameter value="System.Data.SqlServerCe.4.0"/>
</parameters>
</defaultConnectionFactory>
</entityFramework>
Max
  • 117
  • 1
  • 1
  • 4
1

I guess problem occurs only when using SQL Express on .NET Framework v4.0.30319 anyways SQL Server Express Local Database Runtime support in SqlClient is added in .NET Framework update 4.0.2 read - http://support.microsoft.com/kb/2544514

Alternately use connection string similar to this

<add name="EmployeeDbContext" connectionString="Data Source=.;Initial Catalog=Employees;AttachDBFileName=|DataDirectory|\Employees.mdf;Integrated Security=True"
         providerName="System.Data.SqlClient" />
Vishwajit G
  • 510
  • 4
  • 9
1
<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> 

above connection string was giving an error but as soon as added"\" before the name of my database the error got resolved.

Try this:

<add name="myconstr" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\mydata.mdf;Integrated Security=True" providerName="System.Data.SqlClient" /> 
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
0

Here is a blog post I found about that error. It is a bit old, but uses the express version of sql server. MSDN Link

That blog post talks about expecting the server name of the sql database to be a local address and not /sqlexpress

There is also this blog post that talks about having an incorrect connection string. So maybe check your connection string to the database. and your problem could be there.

Rajeev Bera
  • 2,021
  • 1
  • 16
  • 30
AndyC
  • 1,325
  • 1
  • 13
  • 23
0

You are using SQL Server Compact 4.0:

Be sure you have installed SQL Server Compact 4.0.

Install VS 2010 SP1 Tools for SQL Server Compact.

Change your connection string to:

  <connectionStrings>
     <add name="MyModelDBContext" connectionString="Data Source=|DataDirectory|mymodel.sdf" providerName="System.Data.SqlServerCe.4.0"/>
  </connectionStrings>

Right click on controllers folder and -> add -> controller

Type:

YourController
MVC-Controller with read/write ...
MyModel
new datacontext -> APP.Models.DatabaseContext
Robin Wieruch
  • 14,900
  • 10
  • 82
  • 107
0

Facing the same issue, I have looked over my connection string and found that "UNC-style" paths e.g. \\MyServer\MyDBServer are not allowed, one has to use the MyServer\MyDBServer syntax for a remote host or the .\MyDBServer syntax for locally hosted DB Servers.

Dr1Ku
  • 2,875
  • 3
  • 47
  • 56