EntityFramework.dll中出现未处理的“System.TypeInitializationException”类型异常

时间:2013-06-16 11:11:47

标签: c# .net entity-framework sqlite

我尝试使用Entity Framework来学习SQLitethis tutorial。但是,我收到了一个错误。

抛出的错误是:

  

EntityFramework.dll中出现未处理的“System.TypeInitializationException”类型异常

     

附加信息:'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>

6 个答案:

答案 0 :(得分:80)

阅读信息:

  

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

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

答案 1 :(得分:4)

检查参考文献中的实体框架参考版本,并确保它与configSections文件中的Web.config节点匹配。在我的情况下,它指向我的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"/>

答案 2 :(得分:0)

只需从Web.Config文件夹转到Main,而不是Views文件夹中的文件夹:

configSections

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

&LT; ..&GT;

调整已安装的EntityFramework的版本,例如像版本 6 .0.0.0“

答案 3 :(得分:0)

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

答案 4 :(得分:0)

当我从控制台应用程序引用一个库项目,并且该库项目使用的是nuget包时,我遇到了这个问题,该包在控制台应用程序中没有被引用。在控制台应用程序中引用相同的软件包有助于解决此问题。

看到内部异常会有所帮助。

答案 5 :(得分:-3)

在静态课堂上, 如果您从xml或reg获取信息,则类会尝试初始化所有属性。因此,您应该控制配置变量是否存在,否则属性将不会初始化,因此类。

检查xml referance变量是否存在, 检查reg referance变量是否存在, 如果他们不在那里,请确保你处理。

相关问题