“System.Data.Entity.Internal.AppConfig"的类型初始化程序在子网站上引发异常

The type initializer for #39;System.Data.Entity.Internal.AppConfig#39; threw an exception on a Sub Website(“System.Data.Entity.Internal.AppConfig的类型初始化程序在子网站上引发异常)

本文介绍了“System.Data.Entity.Internal.AppConfig"的类型初始化程序在子网站上引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 2 个网站,一个是另一个的子目录,但它是一个应用程序例如:/root &/root/服务

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

他们都使用 Entity Framework 6.x,但子网站抛出 System.Data.Entity.Internal.AppConfig' 的类型初始化程序抛出异常,因为它似乎看到了许多条目由于嵌套的 web.config,对于同一个 EF 数据库提供程序

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>

但我不想这样做,因为不是每个环境都会有嵌套网站.NuGet 倾向于将其放回原处.我可以通过编程方式进行调整吗?

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.RepositoryRepositoryBase.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: 

推荐答案

在 EF6 中您可以使用 Code Base 配置 - 看看 这篇文章了解更多详情.

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

编辑

我签入了 更改 到 EF6 代码,其中忽略了完全重复的代码.这应该可以解决您的问题.请注意,这不适合 6.0.2 版本,应该包含在 6.0.2 之后的下一个版本中.

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.

这篇关于“System.Data.Entity.Internal.AppConfig"的类型初始化程序在子网站上引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:“System.Data.Entity.Internal.AppConfig"的类型初始化程序在子网站上引发异常

基础教程推荐