尝试使用MySQL Entity Framework 6运行控制台应用程序时出现ConfigurationErrorException

我正在尝试学习如何在MySQL中使用Entity Framework 6。 由于我已经有了一个现有的数据库,所以我遵循Model First方法。 但是,我总是面对这个有用的错误消息。

在Program.cs中的这行代码中触发错误:

List students = dbContext.Students.ToList(); 

错误消息只是说:

System.Configuration.dll中发生未处理的“System.Configuration.ConfigurationErrorsException”类型的exception附加信息:无法识别的属性“name”。

我的第一个假设是我的App.config文件有问题。 但是,我不知道它正在讨论的App.config文件中的哪个name属性。 我试图寻找现有的答案或可能的解决方案无济于事所以我决定发布我的问题希望你们帮助我。


我在用什么:

  • Visual Studio 2013(安装在我的电脑中)
  • MySQL Server 5.6(安装在我的电脑上)
  • entity framework6.1.1(通过NuGet包管理器安装在我的项目中)
  • MySql.ConnectorNET.Data 6.8.3.2(通过NuGet包管理器安装在我的项目中)
  • MySql.ConnectorNet.Entity 6.8.3.2(通过NuGet包管理器安装在我的项目中)

我做了什么步骤:

  1. 创建了一个新的控制台应用程序
  2. 通过NuGet包管理器将已安装的Entity Framework,MySql.ConnectorNET.Data和MySql.ConnectorNet.Entity导入到项目中
  3. 使用我现有的数据库添加了名为DSWS_Model的ADO.Net实体数据模型
  4. 输入一些简单的代码,迭代并将学生列表打印到控制台中
  5. 然后出现了愚蠢的运行时错误

Program.cs中

 namespace MySQLEntityFramework6 { class Program { static void Main(string[] args) { Console.WriteLine("Start?"); Console.ReadLine(); try { using (dswsEntities dbContext = new dswsEntities()) { List students = dbContext.Students.ToList(); foreach (Student student in students) { Console.WriteLine(student.name); } } } catch (Exception ex) { ExceptionUtility.LogException(ex); } Console.ReadLine(); } } } 

DSWS_Model.Context.cs

 namespace MySQLEntityFramework6 { using System; using System.Data.Entity; using System.Data.Entity.Infrastructure; public partial class dswsEntities : DbContext { public dswsEntities() : base("name=dswsEntities") { } protected override void OnModelCreating(DbModelBuilder modelBuilder) { throw new UnintentionalCodeFirstException(); } public virtual DbSet Administrators { get; set; } public virtual DbSet BuildingSettings { get; set; } public virtual DbSet Evaluations { get; set; } public virtual DbSet LockerReservations { get; set; } public virtual DbSet Lockers { get; set; } public virtual DbSet LoginQueues { get; set; } public virtual DbSet Students { get; set; } } } 

App.config中

     

错误日志

  Exception Type: System.Configuration.ConfigurationErrorsException Exception: Unrecognized attribute 'name'. (C:\Users\Nico\Documents\Visual Studio 2013\Projects\MVA\Entity Framework\MySQLEntityFramework6\MySQLEntityFramework6\bin\Debug\MySQLEntityFramework6.vshost.exe.Config line 23) Source: System.Configuration Stack Trace: 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.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, 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.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName) at System.Configuration.ConfigurationManager.GetSection(String sectionName) at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName) at System.Data.Common.DbProviderFactories.Initialize() at System.Data.Common.DbProviderFactories.GetFactory(String providerInvariantName) at System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key, Func`3 handleFailedLookup) at System.Data.Entity.Infrastructure.DependencyResolution.DefaultProviderFactoryResolver.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.c__DisplayClass1.b__0(Tuple`2 k) at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory) at System.Data.Entity.Infrastructure.DependencyResolution.CachingDependencyResolver.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.c__DisplayClass3.b__0(IDbDependencyResolver r) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.RootDependencyResolver.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.c__DisplayClass3.b__0(IDbDependencyResolver r) at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext() at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source, Func`2 predicate) at System.Data.Entity.Infrastructure.DependencyResolution.ResolverChain.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.CompositeResolver`2.GetService(Type type, Object key) at System.Data.Entity.Infrastructure.DependencyResolution.DbDependencyResolverExtensions.GetService[T](IDbDependencyResolver resolver, Object key) at System.Data.Entity.Core.EntityClient.EntityConnection.ChangeConnectionString(String newConnectionString) at System.Data.Entity.Core.EntityClient.EntityConnection..ctor(String connectionString) at System.Data.Entity.Internal.LazyInternalConnection.InitializeFromConnectionStringSetting(ConnectionStringSettings appConfigConnection) at System.Data.Entity.Internal.LazyInternalConnection.TryInitializeFromAppConfig(String name, AppConfig config) at System.Data.Entity.Internal.LazyInternalConnection.Initialize() at System.Data.Entity.Internal.LazyInternalConnection.CreateObjectContextFromConnectionModel() at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() at System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at MySQLEntityFramework6.Program.Main(String[] args) in c:\Users\Nico\Documents\Visual Studio 2013\Projects\MVA\Entity Framework\MySQLEntityFramework6\MySQLEntityFramework6\Program.cs:line 19 Target Site: System.Object EvaluateOne(System.String[], System.Configuration.SectionInput, Boolean, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object) 

提前谢谢那些回复的人。

从堆栈跟踪中可以看出,它正在抱怨配置文件中某个DbProviderFactory元素上的’name’属性。 您不需要在remove元素上指定name,只需要不变。 尝试删除它,事情应该开始工作!