“System.Data.Entity.Internal.AppConfig”的类型初始化程序在子网站上抛出异常 [英] The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception on a Sub Website

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

问题描述

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



他们都使用Entity Framework 6.x,但是子网站正在抛出 System.Data.Entity.Internal.AppConfig的类型初始化程序'因为嵌套的web.config



是否有一种方法可以清除提供者收集使我不会得到这个错误?我尝试过没有效果。



如果我评论提供者部分,它的作品

 <&提供商GT; 
< provider invariantName =System.Data.SqlClienttype =System.Data.Entity.SqlServer.SqlProviderServices,EntityFramework.SqlServer/>
< / providers>
< / entityFramework>

但是我不想这样做,因为每个环境都不会有嵌套的网站。 NuGet可以将其重新放入。
我可以以编程方式调整吗?



这是完整的例外和堆栈跟踪

  System.TypeInitializationException被用户代码未处理
HResult = -2146233036
Message = **'System.Data.Entity.Internal的类型初始化程序.AppConfig'抛出一个异常**
Source = EntityFramework
TypeName = System.Data.Entity.Internal.AppConfig
StackTrace:
在System.Data.Entity.Internal。 SystemConfig.getDefaultInstance co.Repository.Data.coContext..ctor()在coModel.Context.Generated.cs中:行23
在co.Repository.RepositoryBase`1.SingleOrDefault(Expression`1谓词)在co.Repository\RepositoryBase .cs:第13行
在UserFactory.GetOneByUserName(String siteCode,String userNa UserFactory.cs中的第151行
UserService中的UserService.GetOneByUserName(String siteCode,String userName):行59
在SyncInvokeGetOneByUserName(Object,Object [],Object [])
在System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance,Object [] inputs,Object []&输出)
在System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
InnerException:System.Configuration.ConfigurationErrorsException
HResult = -2146232062
消息=创建时出错entityFramework的配置部分处理程序:**在应用程序配置中多次指定不变名称System.Data.SqlClient的提供程序。对于每个配置的提供程序,不变名称必须是唯一的。**(web.config line 339)
Source = System.Configuration
BareMessage =创建entityFramework的配置部分处理程序时出错:提供不变量名称'System.Data.SqlClient'在应用程序配置中被多次指定。每个已配置的提供程序的不变名称必须是唯一的。
Filename = web.config
Line = 339
StackTrace:
在System.Configuration.BaseConfigurationRecord.EvaluateOne(String [] keys,SectionInput input,Boolean isTrusted,FactoryRecord factoryRecord,SectionRecord在System.Configuration.BaseConfigurationRecord.Evaluate(FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentResult,Boolean getLkg,Boolean getRuntimeObject,Object& result,Object& resultRuntimeObject)中的
$ System.Configuration中的
。 BaseConfigurationRecord.GetSectionRecursive(String configKey,Boolean getLkg,Boolean checkPermission,Boolean getRuntimeObject,Boolean requestIsHere,Object& result,Object& resultRuntimeObject)
在System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
在系统。 Configuration.ConfigurationManager.GetSection(String sectionName)
在System.Data.Entity.Internal.Ap pConfig..ctor()
在System.Data.Entity.Internal.AppConfig..cctor()
InnerException:System.InvalidOperationException
HResult = -2146233079
Message =提供者在应用程序配置中多次指定不变名称System.Data.SqlClient。每个已配置的提供程序的不变名称必须是唯一的。
Source = EntityFramework
StackTrace:
在System.Data.Entity.Internal.ConfigFile.ProviderCollection.BaseAdd(ConfigurationElement元素)
在System.Configuration.ConfigurationElementCollection.OnDeserializeUnrecognizedElement(String elementName ,XmlReader reader)
在System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader,Boolean serializeCollectionKey)
在System.Configuration.ConfigurationElement.DeserializeElement(XmlReader reader,Boolean serializeCollectionKey)
在System.Configuration .RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionImpl(RuntimeConfigurationRecord configRecord,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentConfig,ConfigXmlReader reader)
在System.Configuration.RuntimeConfigurationRecord.RuntimeConfigurationFactory.CreateSectionWithRestrictedPermissions(Run在System.Configuration.RuntimeConfigurationRecord.CreateSection(Boolean inputIsTrusted,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentConfig,ConfigXmlReader reader)中的
在系统上
时,可以使用以下命令:timeConfigurationRecord configRecord,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentConfig,ConfigXmlReader reader) Configuration.BaseConfigurationRecord.CallCreateSection(Boolean inputIsTrusted,FactoryRecord factoryRecord,SectionRecord sectionRecord,Object parentConfig,ConfigXmlReader reader,String filename,Int32 line)
InnerException:


解决方案

在EF6中,您可以使用代码库配置 - 查看这篇文章了解更多详情。



编辑



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


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: 

解决方案

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.

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

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆