asp.net mvc Invalid value for key 'attachdbfilename'

故事扮演 提交于 2019-11-27 03:26: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?


回答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.




回答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




回答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




回答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>



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



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



回答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



回答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.




回答9:


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.



来源:https://stackoverflow.com/questions/12135081/asp-net-mvc-invalid-value-for-key-attachdbfilename

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