【问题标题】:The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception on a Sub Website“System.Data.Entity.Internal.AppConfig”的类型初始化程序在子网站上引发异常
【发布时间】:2013-11-17 01:02:25
【问题描述】:

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

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

有没有办法清除提供程序集合,这样我就不会收到此错误?我试过放哪个没有效果。

如果我注释掉提供者部分,它会起作用

<providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>

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

这是完整的异常和堆栈跟踪

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: 

【问题讨论】:

    标签: c# entity-framework nuget entity-framework-6


    【解决方案1】:

    我遇到了同样的问题。

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

    版本=5.0.0.0

    到:

    版本=6.0.0.0

    示例:

    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, 
    
    PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    

    【讨论】:

      【解决方案2】:

      我在使用嵌套站点的 EF 4.3.1 时遇到问题。

      两个站点都使用相同的库和连接字符串...错误是由明显的重复引起的

      <connectionStrings>
      

      可能是从父站点加载连接字符串,然后加载子站点的字符串失败。

      在您的子站点中,添加:

      <connectionStrings>
      <clear/>
      ...your normal connection strings
      </connectionStrings>
      

      【讨论】:

      • 花了大约两周时间弄清楚 prod 上发生了什么,我现在可以安息了!
      • 你很棒。我浪费了 6 小时,终于成功了……在父母 -- 孩子应用方面
      【解决方案3】:

      在 EF6 中,您可以使用代码库配置 - 请查看 this 文章了解更多详细信息。

      编辑

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

      【讨论】:

      • 谢谢,我看到了那篇文章。但它没有解释如何检查和修改providers元素,只解释了connectionFactory。
      • @Brian - 我不认为你可以对配置文件本身做任何事情 - 那里没有“添加/删除”逻辑(例如,就像 connectionStrings 所具有的那样)。但是,您可以使用代码库配置以编程方式进行配置。无论如何,您可以发布异常详细信息(堆栈跟踪、完整消息和内部异常,如果有的话)吗?我想看看它到底是从哪里抛出的——也许是删除 EF 代码中的重复项?
      • 我已经添加了堆栈跟踪
      • 谢谢。据我所知,支票是故意的。我认为您可以做的是在配置的 appSettings 部分中指定一个设置,然后在运行时检查(例如在 DbConfiguration 派生类 ctor 或DbConfiguration.Loaded 事件中)是否设置并使用它。我知道这只是一种解决方法,但希望它对您有用。随时在entityframework.codeplex.com/WorkItem/Create 上打开一个关于此的错误以重新考虑当前的设计。
      • 根据关于 codeplex 的错误报告这应该在 6.1.0 中修复
      猜你喜欢
      • 1970-01-01
      • 2015-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-23
      • 2020-02-25
      • 1970-01-01
      • 2016-03-27
      相关资源
      最近更新 更多