An unhandled exception of type "System.TypeInitializationException Exception" occurred in EntityFramework.dll

I tried to learn Entity Framework and SQLite using this tutorial . However, I am getting an error.

Error thrown:

An unhandled exception of type "System.TypeInitializationException Exception" occurred in EntityFramework.dll

Additional information: the type initializer for "System.Data.Entity.Internal.AppConfig" made an exception.

Here is a complete error trace:

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 

Here is the C # code:

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

Here is the App.config file:

 <?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> 

Here is the packages.config file:

 <?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> 
+45
c # sqlite entity-framework
Jun 16 '13 at 11:11
source share
6 answers

Read the message:

Only one <configSections> config element is <configSections> , and if it should be the first child of the root <configuration> element.

Move the configSections element to the beginning - just above where system.data is located.

+80
Jun 16 '13 at 11:19
source share

Check which version of the Entity Framework link you have in your links and make sure it matches your configSections node file in Web.config . In my case, it pointed to version 5.0.0.0 in my configurations, and my link was 6.0.0.0. I just changed it and it worked ...

 <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/> 
+4
May 7, '15 at 4:35
source share

Just go to Web.Config from Main , and not into the Views folder. Folder:

configSections

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

<..>

CUSTOMIZING THE VERSION OF THE ENTRYFRAMEWORKS YOU INSTALLED, FOR example like Version 6 .0.0.0 "

0
Dec 26 '16 at 16:22
source share

Make sure your project has the correct version. For example. The DLL he complains about may be from an older version and that there may be a version mismatch.

0
Aug 01 '17 at 15:26
source share

I had this problem when I referenced the library project from the console application, and the library project used the nuget package, which was not mentioned in the console application. The link to the same package in the console application helped solve this problem.

Seeing Inner Exception can help.

0
Dec 21 '18 at 9:40
source share

In a static class, if you get information from xml or reg, the class tries to initialize all the properties. therefore, you must control whether there is a configuration variable, otherwise the properties will not be initialized by such a class.

Check the variable refer xml is, Check the variable of the reference variable is, Make sure that you work if they are not there.

-3
Jul 23 '17 at 12:33
source share



All Articles