在应用程序配置文件中找不到名为“NorthwindConnection”的连接字符串

时间:2014-08-30 13:28:46

标签: c# entity-framework

我有2个项目:Console app和dll。在DLL中我使用EntityFramework。我有这个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.SqlConnectionFactory, EntityFramework" />
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
      <connectionStrings>
        <add name="NorthwindConnection" connectionString="Data Source=ВЛАДИСЛАВ-ПК\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password=sa;" providerName="System.Data.SqlClient" />
      </connectionStrings>
    </configuration>

我从控制台应用程序调用EntityFramework,但是我有这样的错误消息:“在应用程序配置文件中找不到名为”NorthwindConnection“的连接字符串。”。

这是控制台app.config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>

1 个答案:

答案 0 :(得分:1)

将.dll配置文件中的以下标记复制到控制台项目app.config

  <connectionStrings>
    <add name="NorthwindConnection" connectionString="Data Source=ВЛАДИСЛАВ-ПК\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password=sa;" providerName="System.Data.SqlClient" />
  </connectionStrings>

所以你的控制台项目中的app.config将是:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <connectionStrings>
    <add name="NorthwindConnection" connectionString="Data Source=ВЛАДИСЛАВ-ПК\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password=sa;"   providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>