16

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 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: 
Pawel
  • 31,342
  • 4
  • 73
  • 104
Brian
  • 1,845
  • 1
  • 22
  • 37

3 Answers3

17

I had the same issue.

I resolved this error by simply updating the version number from:

Version=5.0.0.0

to:

Version=6.0.0.0

Example:

<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, 

PublicKeyToken=b77a5c561934e089" requirePermission="false" />
Carrie Kendall
  • 11,124
  • 5
  • 61
  • 81
Scott Nimrod
  • 11,206
  • 11
  • 54
  • 118
17

I had an issue with EF 4.3.1 with a nested site.

Both sites were using the same library and connection strings...the error was caused by apparent duplicates in the

<connectionStrings>

It was presumably loading the connection strings from the parent site, and then failing to load the sub-site's strings.

In your sub-site, add:

<connectionStrings>
<clear/>
...your normal connection strings
</connectionStrings>
Andrew Arace
  • 457
  • 4
  • 11
4

In EF6 you can use Code Base configuration - take a look at this article for more details.

EDIT

I checked in a change to EF6 code where exact duplicates are ignored. This should solve your problem. Note that this did not fit in the 6.0.2 release and should be included in the next release after 6.0.2.

Pawel
  • 31,342
  • 4
  • 73
  • 104
  • Thanks, I saw that article. But it doesn't explain how to check and modify the providers element, only the connectionFactory. – Brian Nov 06 '13 at 15:15
  • @Brian - I don't think you can do anything to the config file itself - there is no "add/remove" logic there (e.g. like the one the connectionStrings have). However you can programmatically configure this using Code Base configuration. Anyways, can you post the exception details (stack trace, full message and inner exceptions, if any)? I would like to see where it is exactly throwing from - maybe it is a matter of removing duplicates in EF code? – Pawel Nov 06 '13 at 17:28
  • I've added the stack trace – Brian Nov 06 '13 at 20:07
  • Thanks. From what I can see the check is intentional. I think what you could do is to specify a setting in the appSettings section in the config and then check at runtime (e.g. in the DbConfiguration derived class ctor or in the `DbConfiguration.Loaded` event) if it is set and use it. I know that this is only a work around but hopefully it will work for you. Feel free to open a bug on this on the https://entityframework.codeplex.com/WorkItem/Create to reconsider the current design. – Pawel Nov 06 '13 at 21:10
  • 1
    According to the bug report on codeplex This should be fixed in 6.1.0 – Brian Dec 09 '13 at 19:56
  • The link is deak – Bastien Vandamme Apr 21 '22 at 07:33