2014-08-30 24 views
2

我有2個項目:控制檯應用程序和dll。在DLL我使用EntityFramework。我有這個app.config文件:在應用程序配置文件中找不到名爲「NorthwindConnection」的連接字符串

<?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> 
+0

找到解決方案!我在控制檯應用程序和dll中添加了EF引用,從dll中刪除了app.config並將app.config添加到控制檯應用程序,並在上面寫入了第一個代碼塊中的數據。 – BJladu4 2014-08-30 14:08:11

回答

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> 
相關問題