asp.net mvc Invalid value for key ‘attachdbfilename’

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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

Method 3

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

Method 4

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>

Method 5

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" />

Method 6

<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" />

Method 7

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

Method 8

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.

Method 9

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x