我正在研究MVC项目。 (现在我正在使用SQL服务器,本地数据库)
我的项目是用MVC项目和数据层项目构建的。 (班级图书馆)
控制器必须从/到视图(MVC)从/到DataLayer获取/传递数据。
我正面临Web.Config和App.Config的问题。
运行项目时出现以下错误。
我很困惑,为什么它在寻找App_Data文件夹?
{"The underlying provider failed on Open."}
{"Cannot attach the file 'D:\\BackUp\\5 may 2014\\MyDBname\\App_Data\\MyDBname.mdf' as database 'MyDBname'."}
这是我的web.config
的代码<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="SchoolContext" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=MyDBname;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
这是我的app.config代码
<?xml version="1.0"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0; Initial Catalog=MyDBname;Integrated Security=SSPI;" providerName="System.Data.SqlClient" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer"/>
</providers>
</entityFramework>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
</configuration>
答案 0 :(得分:0)
尝试使用这样的连接字符串:
“Data Source =。\ SQLEXPRESS; AttachDbFilename = | DataDirectory | DatabaseName.mdf; Integrated Security = True; User Instance = True