C# “System.Data.Entity.Internal.AppConfig”的类型初始值设定项在子网站上引发异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19801125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:55:16  来源:igfitidea点击:

The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception on a Sub Website

c#entity-frameworknugetentity-framework-6

提问by Brian

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

我有 2 个网站,一个是另一个的子目录,但是是一个应用程序,例如:/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 exceptionbecause it appears to be seeing to many entries for the same EF Database Provider because of the nested web.config

它们都使用 Entity Framework 6.x,但子网站正在抛出System.Data.Entity.Internal.AppConfig' 的类型初始值设定项引发异常,因为它似乎看到同一个 EF 数据库提供程序的许多条目,因为嵌套网页配置

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?

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

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: 

采纳答案by Pawel

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

在EF6你可以使用代码基本配置-看看这个文章的更多细节。

EDIT

编辑

I checked in a changeto 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.

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

回答by Scott Nimrod

I had the same issue.

我遇到过同样的问题。

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

我通过简单地更新版本号解决了这个错误:

Version=5.0.0.0

版本=5.0.0.0

to:

到:

Version=6.0.0.0

版本=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" />

回答by Andrew Arace

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

我有一个嵌套站点的 EF 4.3.1 问题。

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>