asp.net mvc Invalid value for key 'attachdbfilename'

删除回忆录丶 提交于 2019-11-28 10:06:50

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.

Teto

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

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

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>

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" />
Neha Choudhary Brar
<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" /> 

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
KingPancake

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.

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.

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