4

Closed

Child Web Application cannot have provider declared in web.config [FixedIn6.1.0-alpha1] [AffectedLastRTM]

description

I have 2 websites, one is a sub directory of another but is an Application ex: /root & /root/Services

They both use Entity Framework 6.x but the child website is throwing The type initializer for System.Data.Entity.Internal.AppConfig' threw an exception because it appears to be seeing to many entries for the same EF Database Provider because of the nested web.config

Is there a way to clear the providers collection so that I do not get this error? I've tried putting in a clear element which had no effect.

If I comment out providers section it works
<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
But I don't want to do this because not every environment is going to have nested websites. and NuGet tends puts it back in. Can I adjust this programmatically?

Here's the full exception and stack trace:
System.TypeInitializationException was unhandled by user code
HResult=-2146233036
Message=**The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception.**
Source=EntityFramework
TypeName=System.Data.Entity.Internal.AppConfig
StackTrace:
     at System.Data.Entity.Internal.AppConfig.get_DefaultInstance()
     at System.Data.Entity.Internal.LazyInternalConnection..ctor(String nameOrConnectionString)
     at System.Data.Entity.DbContext..ctor(String nameOrConnectionString)
     at co.Repository.Data.coContext..ctor() in coModel.Context.Generated.cs:line 23
     at co.Repository.RepositoryBase`1.SingleOrDefault(Expression`1 predicate) in co.Repository\RepositoryBase.cs:line 13
     at UserFactory.GetOneByUserName(String siteCode, String userName) in UserFactory.cs:line 151
     at UserService.GetOneByUserName(String siteCode, String userName) in UserService.cs:line 59
     at SyncInvokeGetOneByUserName(Object , Object[] , Object[] )
     at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
     at System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
InnerException: System.Configuration.ConfigurationErrorsException
     HResult=-2146232062
     Message=An error occurred creating the configuration section handler for entityFramework: **The provider for invariant name 'System.Data.SqlClient' is specified multiple times in the application configuration. The invariant name must be unique for each configured provider.** (web.config line 339)
     Source=System.Configuration
     BareMessage=An error occurred creating the configuration section handler for entityFramework: The provider for invariant name 'System.Data.SqlClient' is specified multiple times in the application configuration. The invariant name must be unique for each configured provider.
     Filename=web.config
     Line=339
     StackTrace:
          at System.Configuration.BaseConfigurationRecord.EvaluateOne(String[] keys, SectionInput input, Boolean isTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult)
          at System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentResult, Boolean getLkg, Boolean getRuntimeObject, Object& result, Object& resultRuntimeObject)
          at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
          at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
          at System.Configuration.ConfigurationManager.GetSection(String sectionName)
          at System.Data.Entity.Internal.AppConfig..ctor()
          at System.Data.Entity.Internal.AppConfig..cctor()
     InnerException: System.InvalidOperationException
          HResult=-2146233079
          Message=The provider for invariant name 'System.Data.SqlClient' is specified multiple times in the application configuration. The invariant name must be unique for each configured provider.
          Source=EntityFramework
          StackTrace:
               at System.Data.Entity.Internal.ConfigFile.ProviderCollection.BaseAdd(ConfigurationElement element)
               at System.Configuration.ConfigurationElementCollection.OnDeserializeUnrecognizedElement(String elementName, XmlReader reader)
               at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
               at System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader, Boolean serializeCollectionKey)
               at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
               at System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(RuntimeConfigurationRecord configRecord, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
               at System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader)
               at System.Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted, FactoryRecord factoryRecord, SectionRecord sectionRecord, Object parentConfig, ConfigXmlReader reader, String filename, Int32 line)
          InnerException: 
Stack Overflow Link
Closed Jan 21 at 7:04 PM by BriceLambson

comments

JeffHandley wrote Nov 9, 2013 at 2:28 AM

@bcbeatty Do you have a proposed solution for this? I'm not sure how NuGet or the NuGet package would know that you're going to deploy into the sub web setup.

divega wrote Nov 9, 2013 at 8:06 AM

Note for triage: as per Jeff's comment, the configuration settings applicable to a specific resource in ASP.NET is based on the aggregation of the web.config files in the nested hierarchy of virtual directories under which the resource is located. At the time the EF NuGet package is installed into an application there is generally no way to anticipate how the application will end up being deployed, i.e. whether it will be located under a sub-directory of another application. The configuration settings, e.g. provider registrations, will get added unconditionally by the NuGet package and if the application is later deployed nested inside another site that also uses EF, then these entries will end up duplicated.

I believe we could choose to handle this in one of three different ways:
1.Have add, remove, and clear "verbs" for the provider entries. Then you can add a "clear" entry on each web.config of an application that can be deployed as nested like this:
<providers>
<clear/>
<add invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>

2.We could ignore duplicate entries in the config as long as they are identical.

3.We can choose not do change anything in this part of the product and just provide guidance to use code-based configuration to register the providers in situations like this.

I am inclined for the latter but if the other alternatives are deemed simple I would be ok with them.

bcbeatty wrote Nov 9, 2013 at 7:10 PM

I see it as an entity framework issue not NuGet.
I would like to see #1 or #2 from divega.

runxc1 wrote Nov 13, 2013 at 9:05 PM

I vote for #1 or #2. When I got the error the first thing I tried to do was <clear /> them out as that is what I have had to do in other places in the same web.config and thought it should work as well.

RoMiller wrote Nov 13, 2013 at 10:46 PM

EF Team Triage: We should at least look at ignoring duplicates. Possibly 'last wins' if it's not too difficult.

moozzyk wrote Dec 3, 2013 at 3:24 AM

Fixed in changeset #d588df2

IHateRepeatingMyselfIHateRepeatingMyself - Ignore duplicate provider config entries (Fixes workitem #1800)

EF only allowed one provider config entry for each provider invariant name. However in case of websites it is possible to have a hierarchy of websites which results in squashing all config settings. This results in duplicate provider entries which caused an exception. The fix is to ignore duplicate entries.

BriceLambson wrote Jan 20 at 8:18 PM

Fixed in changeset d588df2e3b26434af7f49d2b8ce995a1e7d624e8