【问题标题】:How to set connection string for Entity Framework in console app如何在控制台应用程序中为实体框架设置连接字符串
【发布时间】:2015-03-02 09:18:39
【问题描述】:

我有一个包含 2 个项目的解决方案:asp 应用程序和控制台应用程序。在我的asp项目的web.config中,我有这个: <connectionStrings> <add name="con" connectionString="Data Source=PC-Flo\SQLExpress2008;Initial Catalog=test;Persist Security Info=True;User ID=lol;Password=lolation" providerName="System.Data.SqlClient" /> </connectionStrings>

我的连接正常。

现在我想连接我的控制台应用程序,所以在我的 App.config 中我放了同样的东西,但我有这个例外:

“System.Data.Entity.Internal.AppConfig”的类型初始化器 抛出异常

感谢您的帮助。

App.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
    <add name="con" connectionString="Data Source=PC-Flo\SQLExpress2008;Initial Catalog=test;Persist Security Info=True;User ID=lol;Password=lolation" providerName="System.Data.SqlClient" />
</connectionStrings>
  <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=" requirePermission="false" />
  </configSections>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.0.0.0" newVersion="4.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

【问题讨论】:

标签: c# asp.net visual-studio-2010 entity-framework


【解决方案1】:

我删除了我所有的文件,然后把它放进去

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>

    </configSections>
    <connectionStrings>
        <add name="con" connectionString="Data Source=PC-Flo\SQLExpress2008;Initial   Catalog=test;Persist Security Info=True;User ID=lol;Password=lolation" providerName="System.Data.SqlClient" />
    </connectionStrings>

</configuration>

终于成功了。感谢您的帮助。

【讨论】:

    猜你喜欢
    • 2013-12-12
    • 1970-01-01
    • 2014-09-09
    • 2014-07-17
    • 2015-10-20
    • 1970-01-01
    • 1970-01-01
    • 2013-06-09
    • 1970-01-01
    相关资源
    最近更新 更多