简体   繁体   中英

Entity Framework unable to find connection string in nested web.config

I'm new to .NET, but I'm using EntityFramework 5.0 and .NET 4.5 I have a website where the connectionStrings in the web.config are maintained in a configSource as follows:

<connectionStrings configSource="ConfigOverrides\overrideConnectionStrings.config">
</connectionStrings>

My website has modules with nested web.config files. These modules specify their own connectionStrings in the nested web.config. Everything was fine until I put a System.Data.EntityClient connection in my ConfigOverrides\\overrideConnectionStrings file. After I did this I would get an error from the module:

No connection string named 'WebsiteEntities' could be found in the application config file.

If I copy the module's connectionString to the one in ConfigOverrides I get an error that there is already a connection string with that name. If I remove the connection string from their nested web.config and just put it in my overrides, it works. However I'm not wanting to maintain all module's connectionSettings in that global override.

Contents of overrideConnectionStrings.config:

<connectionStrings>
   <add name="SqlServices" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=my_db;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\my_db.mdf;" />
   <add name="TermsEntities" providerName="System.Data.EntityClient" connectionString="metadata=res://*/Terms.csdl|res://*/Terms.ssdl|res://*/Terms.msl;provider=System.Data.SqlClient;provider connection string='data source=(LocalDb)\v11.0;Initial Catalog=my_db;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\my_db.mdf;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework'" />
   <add name="ADServer" connectionString="LDAP://ldap.localdomain:389/DC=company,DC=com" />
 </connectionStrings>

Contents of module's nested Web.config connectionStrings:

<connectionStrings>
   <add name="WebsiteEntities" connectionString="metadata=res://*/WSE.csdl|res://*/WSE.ssdl|res://*/WSE.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.;initial catalog=WSE_DB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
   <add name="RoutingConn" providerName="System.Data.SqlClient" connectionString="data source=.;initial catalog=WSE_DB;integrated security=True;" />
 </connectionStrings>

I should note that the module worked fine until I added my TermsEntities to the main site's web.config (via the ConfigOverrides).

I am still unable to figure out what in my application setup is causing the connectionString to not be found. While debugging I could view the connectionStrings available to the path of the request and I would see all connectionStrings (those in the nested web.config and those in the root web.config).

My workaround is to just use the ConfigurationManager from System.Configuration and read in the connection string :

public partial class WebsiteEntities : DbContext
{
  public WebsiteEntities()
            : base(ConfigurationManager.ConnectionStrings["WebsiteEntities"].ConnectionString ?? "name=WebsiteEntities")
        {
        }
...

This seems to be working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM