'System.Data.Entity.Internal.AppConfig'的类型初始化程序在子网站上引发了一个例外

时间:2013-11-05 23:26:01

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

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

他们都使用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: 

3 个答案:

答案 0 :(得分:17)

我遇到了同样的问题。

我通过简单地更新版本号来解决此错误:

  

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

答案 1 :(得分:17)

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

两个站点都使用相同的库和连接字符串......错误是由

中的明显重复引起的
<connectionStrings>

可能是从父网站加载连接字符串,然后无法加载子网站的字符串。

在您的子站点中,添加:

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

答案 2 :(得分:4)

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

修改

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