EF6.0中App.Config文件连接数据库的问题

tmxk9156 2014-05-22 09:04:15
以前没接触过EF和CodeFirst,昨天看微软的官网上一个例子,通过EF创建数据库和表,网址如下:
通过实体框架Code First建立新数据库
在我这边运行时提示数据库连接错误,VisualStudio版本是VS2010旗舰版,数据库SQL2008R2,最初始的app.config内容如下:

<?xml version="1.0" encoding="utf-8"?>
<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>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>

把 <parameter value="v11.0" />这段改成10.0,加上连接数据库的字符串,还是一样提示数据库连接失败,后来在网站上找了一些例子,把app.config文件改成如下:

<?xml version="1.0" encoding="utf-8"?>
<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>
<connectionStrings>
<add name="BloggingContext" providerName="System.Data.SqlClient"
connectionString="Data Source=local;Integrated Security=SSPI;" />
</connectionStrings>
</configuration>

依然提示连接数据库失败,请教下各位应该如何解决?
...全文
204 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
dalianzsl 2014-05-28
  • 打赏
  • 举报
回复
我觉得是连接字符串写的有问题,要连接的数据库名、数据库服务器的用户名和密码都没有,你改成下面这句话试试 <connectionStrings> <add name="BloggingContext" connectionString="Data Source=localhost;Initial Catalog=要连接的数据库名;Integrated Security=SSPI;User ID=用户名;Password=密码" providerName="System.Data.SqlClient"/> </connectionStrings> 如果没有密码,就这样写 <connectionStrings> <add name="BloggingContext" connectionString="Data Source=localhost;Initial Catalog=要连接的数据库名;Integrated Security=SSPI;User ID=用户名" providerName="System.Data.SqlClient"/> </connectionStrings>

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧