JavaEar 专注于收集分享传播有价值的技术资料

System.InvalidOperationException:在应用程序配置文件中找不到名为" SALESDBEntities" 的连接字符串 (System.InvalidOperationException: No connection string named 'SALESDBEntities' could be found in the application config file)

This is quite confusing.

I have a solution with a main project and a class library. All of my DbContexts are inside the class library. I manually copy the connection strings for the contexts from the class library to the main project. I used this setup for many projects without issue, except for one project.

app.config for Class Library:

<?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" />
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="Common_Code.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="SALESDBEntities" connectionString="metadata=res://CommonCode/Datasets.Contexts.Sales_Data.csdl|res://CommonCode/Datasets.Contexts.Sales_Data.ssdl|res://CommonCode/Datasets.Contexts.Sales_Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server2\SQLEXPRESS;initial catalog=SALESDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="DESIGNDBEntities" connectionString="metadata=res://CommonCode/Datasets.Contexts.Design_Data.csdl|res://CommonCode/Datasets.Contexts.Design_Data.ssdl|res://CommonCode/Datasets.Contexts.Design_Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server2\SQLEXPRESS;initial catalog=DESIGNDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="PRODDBEntities" connectionString="metadata=res://CommonCode/Datasets.Contexts.Manufacturing.Manufacturing_Data.csdl|res://CommonCode/Datasets.Contexts.Manufacturing.Manufacturing_Data.ssdl|res://CommonCode/Datasets.Contexts.Manufacturing.Manufacturing_Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server2\SQLEXPRESS;initial catalog=PRODDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="QCDBEntities" connectionString="metadata=res://CommonCode/Datasets.Contexts.QC_Data.csdl|res://CommonCode/Datasets.Contexts.QC_Data.ssdl|res://CommonCode/Datasets.Contexts.QC_Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=S612SRVR1\SQLEXPRESS;initial catalog=QCDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <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>
</configuration>

app.config for Main Project:

<?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" />
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="Common_Code.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    </sectionGroup>
  </configSections>
  <connectionStrings>
    <add name="SALESDBEntities" connectionString="metadata=res://CommonCode/Datasets.Contexts.Sales_Data.csdl|res://CommonCode/Datasets.Contexts.Sales_Data.ssdl|res://CommonCode/Datasets.Contexts.Sales_Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server2\SQLEXPRESS;initial catalog=SALESDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="DESIGNDBEntities" connectionString="metadata=res://CommonCode/Datasets.Contexts.Design_Data.csdl|res://CommonCode/Datasets.Contexts.Design_Data.ssdl|res://CommonCode/Datasets.Contexts.Design_Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server2\SQLEXPRESS;initial catalog=DESIGNDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="PRODDBEntities" connectionString="metadata=res://CommonCode/Datasets.Contexts.Manufacturing.Manufacturing_Data.csdl|res://CommonCode/Datasets.Contexts.Manufacturing.Manufacturing_Data.ssdl|res://CommonCode/Datasets.Contexts.Manufacturing.Manufacturing_Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=server2\SQLEXPRESS;initial catalog=PRODDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    <add name="QCDBEntities" connectionString="metadata=res://CommonCode/Datasets.Contexts.QC_Data.csdl|res://CommonCode/Datasets.Contexts.QC_Data.ssdl|res://CommonCode/Datasets.Contexts.QC_Data.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=S612SRVR1\SQLEXPRESS;initial catalog=QCDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <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>
</configuration>

They are quite literally identical.

According to my nLog the exception I'm getting is:

System.InvalidOperationException: No connection string named 'SALESDBEntities' could be found in the application config file.

I cannot for the life of me figure out why this exception keeps happening in this project. Both connection strings are exactly the same and work on other projects referencing this same class library.

1个回答

    最佳答案

  1. 英文原文

    I have found the solution to my own problem.

    I noticed that the app.config and appname.exe.config files were not the same size and did not contain the same content.

    I had to clear the bin/Debug and obj/Debug directories for the files to finally match.


    中文翻译

    我找到了解决自己问题的方法。

    我注意到 app.config appname.exe.config 文件的大小不同,并且不包含相同的内容。

    我必须清除 bin / Debug obj / Debug 目录才能最终匹配文件。

    I have found the solution to my own problem.

    I noticed that the app.config and appname.exe.config files were not the same size and did not contain the same content.

    I had to clear the bin/Debug and obj/Debug directories for the files to finally match.

    我找到了解决自己问题的方法。

    我注意到 app.config appname.exe.config 文件的大小不同,并且不包含相同的内容。

    我必须清除 bin / Debug obj / Debug 目录才能最终匹配文件。