发生了类型为“System.TypeInitializationException”的未处理异常,位于EntityFramework.dll中。

47
我正在尝试学习Entity FrameworkSQLite,使用这个教程。然而,我遇到了一个错误。
抛出的错误是:

未处理的异常类型'System.TypeInitializationException'在EntityFramework.dll中发生

额外信息:System.Data.Entity.Internal.AppConfig的类型初始化程序引发了异常。

以下是完整的错误跟踪:
System.TypeInitializationException: The type initializer for 'System.Data.Entity.Internal.AppConfig' threw an exception. ---> System.Configuration.Configuration
ErrorsException: Configuration system failed to initialize ---> System.Configuration.ConfigurationErrorsException: Only one <configSections> element allowed per config file and if present must be the first child of the root <configuration>
element. (C:\Users\Ankur\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe.config line 11)
   at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
   at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
   at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   --- End of inner exception stack trace ---
   at System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)
   at System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)
   at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
   at System.Configuration.ConfigurationManager.get_ConnectionStrings()
   at System.Data.Entity.Internal.AppConfig..ctor()
   at System.Data.Entity.Internal.AppConfig..cctor()
   --- End of inner exception stack trace ---
   at System.Data.Entity.Internal.AppConfig.get_DefaultInstance()
   at System.Data.Entity.Internal.LazyInternalConnection..ctor(String nameOrConnectionString)
   at System.Data.Entity.DbContext..ctor()
   at ConsoleApplication1.ChinookContext..ctor()
   at ConsoleApplication1.Program.Main(String[] args) in c:\Users\Ankur\Documents\Visual Studio 2012\Projects\ConsoleApplication1\ConsoleApplication1\Program.cs
:line 16

这里是C#代码:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new ChinookContext()) //error comes on this line
            {
            }
        }
    }

    class ChinookContext : DbContext
    {
    }
}

这里是文件App.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.data>
    <DbProviderFactories>
      <add name="SQLite Data Provider" invariant="System.Data.SQLite" description="Data Provider for SQLite" type="System.Data.SQLite.SQLiteFactory, System.Data.SQLite" />
    </DbProviderFactories>
  </system.data>
  <connectionStrings>
    <add name="ChinookContext" connectionString="Data Source=|DataDirectory|Chinook_Sqlite_AutoIncrementPKs.sqlite" providerName="System.Data.SQLite" />
  </connectionStrings>
  <configSections>
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
</configuration>

这里是文件packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="EntityFramework" version="5.0.0" targetFramework="net45" />
  <package id="System.Data.SQLite.x86" version="1.0.86.0" targetFramework="net45" />
</packages>

你是怎么获取错误追踪的?我需要看一个类似问题的追踪,但找不到。 - puser
6个回答

81

读取消息:

每个配置文件只允许一个 <configSections> 元素,如果存在,则必须是根 <configuration> 元素的第一个子元素。

将 configSections 元素移到顶部 - 就在当前 system.data 元素上方。


2
你好,我可以问一个问题吗?我的设置和问题描述中一样,在添加了你所描述的更改后,我遇到了以下错误:在 System.Data.dll 中发生了未处理的类型为“System.Configuration.ConfigurationErrorsException”的异常。 其他信息:无法找到或加载已注册的 .Net Framework 数据提供程序。 - Azaryan
@Azaryan 我不知道,我能建议的是:阅读完整的错误消息(包括任何内部异常) - Marc Gravell
我也遇到了这个问题,错误消息确实解释了问题,但我的configSection确实是在Configuration元素下的第一个节点。事实证明,问题出在一个被引用的dll中,它没有将其作为第一个节点。 - McGaz

4

请检查您在References中引用的 Entity Framework 版本,并确保其与您的 Web.config 文件中的 configSections 节点匹配。在我的情况下,它指向我的 configSections 中的版本 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"/>

0

检查您的项目中是否引用了正确的版本。例如,它抱怨的dll可能来自旧版本,这就是为什么可能存在版本不匹配的原因。


0
只需前往Main文件夹下的Web.Config,而不是Views文件夹中的一个:

configSections

section name="entityFramework" type="System.Data....,Version="<strong>5</strong>.0.0.0"..

<..>

调整已安装的 EntityFramework 版本,例如版本6.0.0.0"


0
我在将一个库项目从控制台应用程序引用时遇到了这个问题,该库项目使用了一个 nuget 包,在控制台应用程序中没有引用。在控制台应用程序中引用相同的包有助于解决此问题。
查看内部异常可以提供帮助。

-3
在静态类中,如果你从xml或者reg获取信息,类会尝试初始化所有的属性。因此,你应该控制config变量是否存在,否则属性将不会被初始化到类中。 检查xml引用变量是否存在, 检查reg引用变量是否存在, 确保你处理它们不存在的情况。

XML和“reg”(什么是“reg”?)的相关性是什么?引用变量是什么?如果它们不存在,你如何处理它们? - CodeCaster
大多数配置从xml文件中读取。如果您要查找的内容不在xml文件中,则类将会给出错误。有些配置文件从reg文件中读取。如果您从reg中读取一些内容,也应该进行检查。Reg表示注册表。 - SonerZ

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接