Une exception non gérée du type 'System.TypeInitializationException' s'est produite dans EntityFramework.dll
J'essayais d'apprendre Entity Framework et SQLite à l'aide de ce tutoriel. Cependant, j'obtiens une erreur.
L'erreur renvoyée est:
Une exception non gérée du type 'System.TypeInitializationException' s'est produite dans EntityFramework.dll
Informations supplémentaires: L'initialiseur de type pour 'Système.Les données.De l'entité.Interne.AppConfig' a déclenché une exception.
On a la trace de l'erreur:
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
Voici le code C#:
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
using (var context = new ChinookContext()) //error comes on this line
{
}
}
}
class ChinookContext : DbContext
{
}
}
Ici est le fichier 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>
Ici est le fichier paquets.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>
- Comment avez-vous la trace de l'erreur? J'ai besoin de voir pour un problème similaire, je vais avoir mais ne peut pas le trouver.
Vous devez vous connecter pour publier un commentaire.
Lire le message:
Déplacer le configSections élément vers le haut juste au-dessus où le système.les données sont actuellement.
Vérifier la version de l'Entité de référence de Cadre que vous avez dans vos Références et assurez-vous qu'il correspond à votre
configSections
nœud dansWeb.config
fichier. Dans mon cas, il a été pointant vers la version 5.0.0.0 dans mon configSections et ma référence était 6.0.0.0. Je viens de changer et cela a fonctionné...Il suffit d'aller à
Web.Config
deMain
dossier, pas celleViews
Dossier:configSections
section
name="entityFramework" type="System.Data. .....,Version=" <strong>5</strong>.0.0.0"..
<..>
AJUSTER LA VERSION DE EntityFramework vous avez installé, ex. comme la Version 6.0.0.0"
Vérifier que la bonne version est référencé dans votre projet. E. g. la dll c'est de se plaindre, peut être à partir d'une ancienne version et c'est pourquoi il pourrait y avoir une incompatibilité de version.
J'ai eu ce problème quand j'ai arbitré un projet de bibliothèque à partir d'une application console, et le projet de bibliothèque a été à l'aide d'un package nuget qui n'est pas référencée dans l'application de console. Se référant à la même package dans l'application de console a aidé à résoudre ce problème.
De voir l'Intérieur exception peut vous aider.
En classe statique,
si vous êtes l'obtention d'informations à partir de xml ou reg, classe essaie d'initialiser toutes les propriétés. par conséquent, vous devez contrôler si la variable config est-il autrement des propriétés initialisera pas tellement la classe.
Vérifier xml referance variable est là,
Vérifier reg referance variable est là,
Assurez-vous de gérer si elles ne sont pas là.