使用MySQL实体框架运行控制台应用程序时出现ConfigurationErrorException
本文关键字:ConfigurationErrorException 应用程序 控制台 MySQL 实体 框架 运行 使用 | 更新日期: 2023-09-27 18:11:34
我正在学习如何使用实体框架6与MySQL。因为我已经有一个现有的数据库,我遵循模型第一的方法。然而,我总是面对这样的错误信息,这是模糊的帮助。
在Program.cs中的这行代码中触发错误:
List<Student> students = dbContext.Students.ToList();
错误信息只是说:
未处理的System.Configuration类型异常。其他信息:无法识别的属性'name'.
我的第一个假设是我的App.config文件有问题。但是,我不知道App.config文件中的哪个name
属性是谈论的。我试着寻找现有的答案或可能的解决方案,但无济于事,所以我决定把我的问题贴出来,希望你们能帮助我。
我在用什么:
- Visual Studio 2013(安装在我的PC上)
- MySQL Server 5.6(安装在我的PC)
- 实体框架6.1.1(安装在我的项目通过NuGet包管理器)
- MySql.ConnectorNET。Data 6.8.3.2(通过NuGet Package Manager安装在我的项目中)
- MySql.ConnectorNet。实体6.8.3.2(通过NuGet Package Manager安装在我的项目中)
我做了什么步骤:
- 创建一个新的控制台应用 已安装的实体框架,MySql.ConnectorNET。数据和MySql.ConnectorNet.Entity通过NuGet包管理器导入到项目中
- 增加一个ADO。使用我现有的数据库 命名为
- 输入一些简单的代码,迭代并打印学生列表到控制台
- 然后愚蠢的运行时错误显示
DSWS_Model
的Net实体数据模型Program.cs
namespace MySQLEntityFramework6
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Start?");
Console.ReadLine();
try
{
using (dswsEntities dbContext = new dswsEntities())
{
List<Student> 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<Administrator> Administrators { get; set; }
public virtual DbSet<BuildingSetting> BuildingSettings { get; set; }
public virtual DbSet<Evaluation> Evaluations { get; set; }
public virtual DbSet<LockerReservation> LockerReservations { get; set; }
public virtual DbSet<Locker> Lockers { get; set; }
public virtual DbSet<LoginQueue> LoginQueues { get; set; }
public virtual DbSet<Student> Students { get; set; }
}
}
App.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="mssqllocaldb" />
</parameters>
</defaultConnectionFactory>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
<provider invariantName="MySql.Data.MySqlClient" type="MySql.Data.MySqlClient.MySqlProviderServices, MySql.Data.Entity.EF6, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"></provider>
</providers>
</entityFramework>
<system.data>
<DbProviderFactories>
<remove name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" />
<add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.8.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
</DbProviderFactories>
</system.data>
<connectionStrings>
<add name="dswsEntities" connectionString="metadata=res://*/DSWS_Model.csdl|res://*/DSWS_Model.ssdl|res://*/DSWS_Model.msl;provider=MySql.Data.MySqlClient;provider connection string="server=localhost;user id=root;password=root;persistsecurityinfo=True;database=dsws"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
错误日志
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.<GetService>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.<GetService>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.<GetService>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<TResult>.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,只需指定不变式即可。试着删除它,事情应该开始工作!